Supported Option Formats
cargs supports multiple option formats to provide maximum flexibility for end users. This flexibility allows your program to handle different command-line conventions and accommodate various user preferences.
Standard Options
Overview
cargs recognizes several standard formats for command-line options, including GNU-style long options, POSIX-style short options, and various combinations.
Long Options
For options with a long name (--option):
Short Options
For options with a short name (-o):
Example
Option Terminator (--)
Tip
The double dash -- is recognized as a terminator indicating the end of options. Everything that follows is treated as positional arguments, even if they start with - or --.
In this example, --file-with-dashes.txt is treated as a positional argument, not as an option.
Multi-Value Collections
cargs supports collection options that can hold multiple values (arrays) or key-value pairs (maps).
Array Option Formats
For array options like OPTION_ARRAY_STRING, OPTION_ARRAY_INT, etc.:
Integer Ranges
Integer array options support a convenient range syntax:
Map Option Formats
For map options like OPTION_MAP_STRING, OPTION_MAP_INT, etc.:
Boolean Maps
For boolean maps (OPTION_MAP_BOOL), values are parsed as booleans:
- True values: "true", "yes", "1", "on", "y" (case-insensitive)
- False values: "false", "no", "0", "off", "n" (case-insensitive)
Positional Arguments
Positional arguments are processed in the order they appear on the command line, after matching all options:
In this example, file1.txt and file2.txt are positional arguments.
Required vs Optional Positionals
cargs distinguishes between required and optional positional arguments:
Warning
Required positional arguments must always be defined before optional ones in your CARGS_OPTIONS definition.
Positional Arguments with Options
Positional arguments can be used along with options in any order:
my_program --verbose input.txt --output=output.txt
my_program input.txt --verbose --output=output.txt
Both commands are equivalent, as cargs identifies positional arguments after matching all options.
Subcommands
cargs supports Git/Docker-style subcommands, allowing for complex command hierarchies:
Basic Subcommand Format
Examples:
Nested Subcommands
cargs also supports nested subcommands for deeper command hierarchies:
Examples:
my_program --debug service create --name myservice --image ubuntu
my_program config set server.port 8080
Command Abbreviations
cargs supports command name abbreviations, allowing users to type shortened versions as long as they are unambiguous:
# These are equivalent if no other command starts with "i":
my_program install package.tgz
my_program inst package.tgz
my_program i package.tgz
# But ambiguous if there are multiple matches
my_program i package.tgz # ERROR if both "install" and "init" exist
Mixing Different Formats
cargs allows mixing different format styles in a single command line:
This command includes:
- A short option flag (-v)
- A long option with value (--output=file.txt)
- Positional arguments (file1.txt file2.txt)
- A subcommand with its own option (add --force extra.txt)
Handling Ambiguous Options
In some cases, command-line input can be ambiguous. cargs resolves ambiguity using these rules:
- An argument starting with a single dash (
-) followed by a single character is treated as a short option - An argument starting with double dashes (
--) is treated as a long option - An argument after
--is always treated as a positional argument - An argument not starting with a dash is treated as a positional argument or subcommand (based on definition)
When parsing combined short options (-abc):
- Each character is treated as a separate boolean option
- If a non-boolean option is encountered, the rest of the string is treated as its value
Command-Line Equivalent Forms
The following command forms are all equivalent with appropriate option definitions:
| Format | Example |
|---|---|
| Long options with equal sign | ./my_program --verbose --output=file.txt input.txt |
| Long options with space | ./my_program --verbose --output file.txt input.txt |
| Short options with space | ./my_program -v -o file.txt input.txt |
| Short options no space | ./my_program -v -ofile.txt input.txt |
| Combined short options | ./my_program -vo file.txt input.txt |
| Combined short with value | ./my_program -vofile.txt input.txt |
Using -- terminator |
./my_program -v -- --input-with-dashes.txt |
| With subcommand | ./my_program -v add --force file.txt |
Important Notes
Warning
- Boolean options (flags) do not require a value; their presence is enough to enable them.
- Combined options (
-abc) only work for boolean options. - For non-boolean options, the last option in a combination can take a value.
Examples
-vo output.txt(enables the-voption and sets-otooutput.txt)-vooutput.txt(enables the-voption and sets-otooutput.txt)-vxooutput.txt(enables the-vand-xoptions, and sets-otooutput.txt)
Note
Positional arguments are always processed in the order defined in CARGS_OPTIONS.