Skip to content

fix CLI aguments #99

@ElectricRCAircraftGuy

Description

@ElectricRCAircraftGuy

Hi, thanks for your tool!

Continuing the discussion from here: https://askubuntu.com/questions/388262/decrypt-axcrypt-encrypted-file/1536042#comment2702481_1524304

How arguments for command-line tools are expected to work in Linux; optional and positional args in Linux

At the top of a help menu, the tool usage should be described like this, for example. Arguments in [] are optional, and arguments in <> are required.

Here, we have a program called my_program that takes an input file and an output file. The output file is optional because, for example, you might auto-generate a name from the input file.

Here, input_file and output_file are both positional arguments. Their position relative to each other only, but not relative to the options, is important.

my_program [options] <input_file> [output_file]

Options are a special type of optionl argument. They are called "flags" or "options". The short form uses - plus a single, case-sensitive letter. The long form uses -- plus a case-sensitive word or phrase.

Example descriptions from a help menu:

-a, --all   Do blah blah blah
-B, --big   Do big blah blah blah

Single-letter options can be joined with a single -. Ex: -aB is equivalent to -a -B or -B -a.

Options/flags do not respect order. They can be placed anywhere in the command line. Positional args respect order and must be placed in the correct order relative to other positional args.

Examples: these are all exactly equivalent calls and the program parser should treat them as such.

my_program -a -B input_file output_file
my_program -B -a input_file output_file
my_program input_file -a --big output_file
my_program input_file -B -a output_file
my_program input_file output_file -a -B
my_program input_file output_file -B -a
my_program -aB input_file output_file
my_program -Ba input_file output_file
my_program -a input_file -B output_file
my_program -B input_file output_file -a
my_program input_file output_file -B --all
my_program input_file output_file --big --all

Notice, again, that the positional arguments must be in the correct order relative to each other. The options can be in any order or position.

If an optional argument has a parameter passed to it, however, then you treat the parameter as a positional argument relative to the option.

Example help menu description:

-o, --output-to <output_file>     Write output to <output_file>

You can also allow an = to separate the option from the parameter:

-o, --output-to <output_file>                   Write output to <output_file>.
-o=<output_file>, --output-to=<output_file>     Same as above. 

Identical usages:

my_program --output-to output_file input_file
my_program --output-to=output_file input_file
my_program input_file --output-to output_file
my_program input_file --output-to=output_file
my_progrm input_file -o output_file

Now put it all together. Identical usages:

my_program -a -B --output-to output_file input_file
my_program --output-to output_file -a -B input_file
my_program -a --output-to=output_file -B input_file
my_program --output-to=output_file -Ba input_file
my_program -o output_file -Ba input_file
my_program -o=output_file -Ba input_file
my_program -Bao output_file input_file
# etc.

NOT allowed, because now it makes output_file attach to the -a option instead of to the -o option:

my_program -Boa output_file input_file

An example bash script that I wrote that respects and handles all of the above (namely: options in any order, positional args in order relative to each other, and options with parameters), except for the = syntax and the clustered option syntax (like -aB), is here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/bash/argument_parsing__3_advanced__gen_prog_template.sh

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions