Argc
A sh/bash cli framework. Generate cli inteface from comments
Get Started
Write the following demo.sh script
# @flag -q --quit Do not print anything to stdout
# @option --grep* <REGEX> Limit the commits output
# @option --color[auto|always|never] Print colorful output
# @arg pathspec+ Files to handle
Argc parses command arguments and prints variables
argc_quit=1
argc_grep=( "FOO" "BAR" "BAZ" )
argc_color="auto"
argc_pathspec=( "README.md" "CHANGELOG.md" )
Argc recognizes -h option and prints help text
demo
USAGE:
demo [OPTIONS] <PATHSPEC>...
ARGS:
<PATHSPEC>... Files to handle
OPTIONS:
--color <COLOR> Print colorful output [default: auto] [possible values: auto, always,
never]
--grep <REGEX>... Limit the commits output
-h, --help Print help information
-q, --quit Do not print anything to stdout
Argc identifies parameter errors and prints error text
argc demo.sh --color=none
error: "none" isn't a valid value for '--color <COLOR>'
[possible values: auto, always, never]
USAGE:
demo --color <COLOR> <PATHSPEC>...
For more information try --help
How Argc works:
- Extract parameter definitions from script comments
- Parse command line arguments
- If the parameter is abnormal, output error text or help information
- If everything is normal, output the parsed parameter variable
res=
if [; then
fi
The above code is too redundant, and Argc provides the -e option to rescue
Tag
Argc generates parsing rules and help documentation based on tags (fields marked with @ in comments).
# @describe A fictional versioning CLI
# @version 2.17.1
# @author nobody <nobody@example.com>
# @flag --no-pager Do not pipe Git output into a pager
# @option --git-dir=.git Set the path to the repository
# @cmd Shows the commit log.
# @arg refspec* Specify what destination ref
test2 2.17.1
nobody <nobody@example.com>
A fictional versioning CLI
USAGE:
test2 [OPTIONS] <SUBCOMMAND>
OPTIONS:
--git-dir <GIT-DIR> Set the path to the repository [default: .git]
-h, --help Print help information
--no-pager Do not pipe Git output into a pager
-V, --version Print version information
SUBCOMMANDS:
help Print this message or the help of the given subcommand(s)
log Shows the commit log.
@describe
# @describe [string]
# @describe A fictional versioning CLI
Define description
@version
# @version [string]
# @version 2.17.1
Define version
@author
# @author [string]
# @author nobody <nobody@example.com>
Define author
@cmd
# @cmd [string]
# @cmd Shows the commit log.
Define subcommand
@option
# @cmd [short] [long][modifer] [notation] [string]
# @option -j, --threads <NUM> Number of threads to use.
# @option --grep* <PATTERN>
# @option --dump-format[=json|yaml]
# @option --shell-arg=-cu
Define value option
modifer
The symbol after the long option name is the modifier, such as * in --grep*
*: occur multiple times, optional+: occur multiple times, required!: required=value: default value[a|b|c]: choices[=a|b|c]: choices, first is default.
notation
Used to indicate that the option is a value option, other than a flag option.
If not provided, the option name is used as a placeholder by default.
You can use placeholder hint option value types <NUM>, <PATH>, <PATTERN>, <DATE>
@flag
# @flag [short] [long] [help string]
## @flag --no-pager
## @flag -q, --quiet Do not print anything to stdout
Define flag option
@arg
# @arg <name>[modifer] [help string]
## @arg pathspec* Files to add content from.
Define positional arguement
modifer
*: occur multiple times, optional+: occur multiple times, required!: required
License
Copyright (c) 2022 argc-developers.
argc is made available under the terms of either the MIT License or the Apache License 2.0, at your option.
See the LICENSE-APACHE and LICENSE-MIT files for license details.