Crates.io | argc |
lib.rs | argc |
version | |
source | src |
created_at | 2022-02-22 02:47:18.236404 |
updated_at | 2024-12-09 09:33:03.237098 |
description | A bash cli framework, also a bash-based command runner |
homepage | https://github.com/sigoden/argc |
repository | https://github.com/sigoden/argc |
max_upload_size | |
id | 537098 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Argc is a powerful Bash framework that simplifies building full-featured CLIs. It lets you define your CLI through comments, focusing on your core logic without dealing with argument parsing, usage text, error messages, and other boilerplate code.
cargo install argc
brew install argc
yay -S argc
Alternatively, download pre-built binaries for macOS, Linux, and Windows from GitHub Releases, extract it, and add the argc
binary to your $PATH
.
You can use the following command on Linux, MacOS, or Windows to download the latest release.
curl -fsSL https://raw.githubusercontent.com/sigoden/argc/main/install.sh | sh -s -- --to /usr/local/bin
install-binary can be used to install argc in a GitHub Actions workflow.
- uses: sigoden/install-binary@v1
with:
repo: sigoden/argc
Building a command-line program using Argc is a breeze. Just follow these two steps:
1. Design Your CLI Interface:
Describe options, flags, positional parameters, and subcommands using comment tags (explained later).
2. Activate Argument Handling:
Add this line to your script: eval "$(argc --argc-eval "$0" "$@")"
. This integrates Argc's parsing magic into your program.
Let's illustrate with an example:
# @flag -F --foo Flag param
# @option --bar Option param
# @option --baz* Option param (multi-occurs)
# @arg val* Positional param
eval "$(argc --argc-eval "$0" "$@")"
echo foo: $argc_foo
echo bar: $argc_bar
echo baz: ${argc_baz[@]}
echo val: ${argc_val[@]}
Run the script with some sample arguments:
./example.sh -F --bar=xyz --baz a --baz b v1 v2
Argc parses these arguments and creates variables prefixed with argc_
:
foo: 1
bar: xyz
baz: a b
val: v1 v2
Just run ./example.sh --help
to see the automatically generated help information for your CLI:
USAGE: example [OPTIONS] [VAL]...
ARGS:
[VAL]... Positional param
OPTIONS:
-F, --foo Flag param
--bar <BAR> Option param
--baz [BAZ]... Option param (multi-occurs)
-h, --help Print help
-V, --version Print version
With these simple steps, you're ready to leverage Argc and create robust command-line programs!
Comment tags are standard Bash comments prefixed with @
and a specific tag. They provide instructions to Argc for configuring your script's functionalities.
Tag | Description |
---|---|
@describe |
Sets the description for the command. |
@cmd |
Defines a subcommand. |
@alias |
Sets aliases for the subcommand. |
@arg |
Defines a positional argument. |
@option |
Defines an option argument. |
@flag |
Defines a flag argument. |
@env |
Defines an environment variable. |
@meta |
Adds metadata. |
See specification for the grammar and usage of all the comment tags.
Generate an independent bash script that incorporates all functionalities typically available when the argc
command is present.
The generated script removes the argc
dependency, enhances compatibility, and enables deployment in a wider range of environments.
argc --argc-build <SCRIPT> [OUTPATH]
argc --argc-build ./example.sh build/
./build/example.sh -h # The script's functionality does not require the `argc` dependency
Argc is a also command runner built for those who love the efficiency and flexibility of Bash scripting.
Similar to how Makefile define commands for the make
tool, Argcscript uses an Argcfile.sh
to store your commands, referred to as "recipes".
Why Choose Argc for Your Projects?
See command runner for more details.
Argc automatically provides shell completions for all argc-based scripts.
argc --argc-completions <SHELL> [CMDS]...
In the following, we use cmd1 and cmd2 as examples to show how to add a completion script for various shells.
# bash (~/.bashrc)
source <(argc --argc-completions bash cmd1 cmd2)
# elvish (~/.config/elvish/rc.elv)
eval (argc --argc-completions elvish cmd1 cmd2 | slurp)
# fish (~/.config/fish/config.fish)
argc --argc-completions fish cmd1 cmd2 | source
# nushell (~/.config/nushell/config.nu)
argc --argc-completions nushell cmd1 cmd2 # update config.nu manually according to output
# powershell ($PROFILE)
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
argc --argc-completions powershell cmd1 cmd2 | Out-String | Invoke-Expression
# xonsh (~/.config/xonsh/rc.xsh)
exec($(argc --argc-completions xonsh cmd1 cmd2))
# zsh (~/.zshrc)
source <(argc --argc-completions zsh cmd1 cmd2)
# tcsh (~/.tcshrc)
eval `argc --argc-completions tcsh cmd1 cmd2`
The core of all completion scripts is to call argc --argc-compgen
to obtain completion candidates.
$ argc --argc-compgen bash ./example.sh example --
--foo (Flag param)
--bar (Option param)
--baz (Option param (multi-occurs))
--help (Print help)
--version (Print version)
So argc is a also completion engine, see argc-completions.
Generate man pages for your argc-based CLI.
argc --argc-mangen <SCRIPT> [OUTDIR]
argc --argc-mangen ./example.sh man/
man man/example.1
The built-in Bash in macOS is version 3.2 (released in 2007), and the known tools (ls, cp, grep, sed, awk, etc.) are based on BSD. For better functionality and compatibility, it is recommended to install Bash version 5 and GNU tools.
Use brew
to install Bash and GNU tools:
brew install bash coreutils gawk gnu-sed grep
And update the PATH
environment variable:
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:/opt/homebrew/opt/gawk/libexec/gnubin:/opt/homebrew/opt/gnu-sed/libexec/gnubin:/opt/homebrew/opt/grep/libexec/gnubin:$PATH"
The only dependency for argc
is Bash. Since most developers working on Windows have Git installed, which includes Git Bash, you can safely use argc
and GNU tools (like grep
, sed
, awk
, etc.) in the Windows environment.
.sh
file executableTo execute a .sh
script file directly like a .cmd
or .exe
file, execute the following code in PowerShell.
# Add .sh to PATHEXT
[Environment]::SetEnvironmentVariable("PATHEXT", [Environment]::GetEnvironmentVariable("PATHEXT", "Machine") + ";.SH", "Machine")
# Associate the .sh file extension with Git Bash
New-Item -LiteralPath Registry::HKEY_CLASSES_ROOT\.sh
New-ItemProperty -LiteralPath Registry::HKEY_CLASSES_ROOT\.sh -Name "(Default)" -Value "sh_auto_file" -PropertyType String -Force
New-ItemProperty -LiteralPath Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Classes\sh_auto_file\shell\open\command `
-Name '(default)' -Value '"C:\Program Files\Git\bin\bash.exe" "%1" %*' -PropertyType String -Force
Copyright (c) 2023-2024 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.