
Ask HN: Unix Utility Development Conventions - rayascott
I&#x27;m slowly hacking away at a zsh script that shows some promise as a command line tool. I want to learn more about the conventions regarding command line tool development in Unix (and&#x2F;or macOS), but don&#x27;t really know where to look for this information.<p>What is the correct way, or convention, to specify and parse command line arguments, for example? How should I package my tool? What is the best way to handle deployment of the various aspects of my tool, for example the man page, or configuration settings? How should I handle the upgrade process?<p>Smaller details, like should I store my source code in the repo with execute permissions turned on, or should I only turn on those permissions when the files are deployed on the user&#x27;s machine? What group should I set as the default for my executable files?<p>Does anyone know any great resources out there that address these issues?
======
jolmg
> What is the correct way, or convention, to specify and parse command line
> arguments, for example?

I often use variations of this pattern:

    
    
      version=1.0.0
    
      usage() {
        cat << EOF
      USAGE: $0 {{-f|--file} <filename>}
    
      Some description.
    
      OPTIONS
        -h, --help
          Display this help.
    
        -v, --version
          Display version.
    
        -f <filepath>, --file <filepath>
          Specify input file.
      EOF
      }
    
      while (( $# )); do
        case "$1" in
          -h|--help)
            usage
            exit
          ;;
    
          -v|--version)
            printf "%s\n" "$version"
            exit
          ;;
    
          -f|--file)
            input_file="$2"
            shift
          ;;
    
          *)
            >&2 printf "Unknown option: %s\n" "$1"
            >&2 usage
            exit 1
          ;;
        esac
        shift
      done
    

There are standard guidelines[1][2].

[1]
[https://www.gnu.org/prep/standards/standards.html#Command_00...](https://www.gnu.org/prep/standards/standards.html#Command_002dLine-
Interfaces)

[2] [https://cli-
guide.readthedocs.io/en/latest/design/guidelines...](https://cli-
guide.readthedocs.io/en/latest/design/guidelines.html#id1)

------
tannhaeuser
[https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01)

[http://catb.org/~esr/writings/taoup/html/ch10s05.html](http://catb.org/~esr/writings/taoup/html/ch10s05.html)

~~~
rayascott
The Art of Unix Programming looks interesting, thanks!

