• Stars
    star
    665
  • Rank 67,810 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Bash CLI framework, also a Bash-based command runner.

Argc

CI Crates

An elegant command-line options, arguments and sub-commands parser for bash.

demo

Install

With cargo

cargo install argc

Binaries on macOS, Linux, Windows

Download from Github Releases, unzip and add argc to your $PATH.

GitHub Actions

extractions/setup-crate can be used to install just in a GitHub Actions workflow.

- uses: extractions/setup-crate@v1
  with:
    owner: sigoden
    name: argc

Usage

To write a command-line program with argc, we only need to do two things:

  1. Describe options, flags, positional parameters and subcommands in comments.
  2. Insert eval "$(argc --argc-eval "$0" "$@")" into script to let argc to parse command line arguments.

Write example.sh

# @flag   --foo   Flag value
# @option --bar   Option value
# @arg baz*       Positional values

eval "$(argc --argc-eval "$0" "$@")"
echo foo: $argc_foo
echo bar: $argc_bar
echo baz: ${argc_baz[@]}

Run ./example.sh --foo --bar=xyz a b c, you can see argc successfully parses arguments and generate variables with argc_ prefix.

foo: 1
bar: xyz
baz: a b c

Run ./example.sh -h, argc wll print help information for you.

USAGE: example.sh [OPTIONS] [BAZ]...

ARGS:
  [BAZ]...  Positional values

OPTIONS:
      --foo        Flag value
      --bar <BAR>  Option value
  -h, --help       Print help

Comment Tags

argc parses cli definition from comment tags.

@cmd

@cmd [string]

Define a subcommand

# @cmd Upload a file
upload() {
  echo Run upload
}

# @cmd Download a file
download() {
  echo Run download
}
USAGE: test.sh <COMMAND>

COMMANDS:
  upload    Upload a file
  download  Download a file

@arg

@arg <name>[modifier|default|modifer+choices] [zero-or-one value notation] [help-string]

Define a positional argument.

# @arg va
# @arg vb!                 requird
# @arg vc*                 multiple
# @arg vd+                 required + multiple
# @arg vna <PATH>          value notation
# @arg vda=a               default
# @arg vdb=`_default_fn`   default from fn
# @arg vca[a|b]            choice
# @arg vcb[=a|b]           choice + default
# @arg vcc*[a|b]           multiple + choice
# @arg vcd+[a|b]           required + multiple + choice
# @arg vfa[`_choice_fn`]   choice from fn
# @arg vfb[?`_choice_fn`]  choice from fn + no validation
# @arg vfc*[`_choice_fn`]  multiple + choice from fn

@option

@option [short] <long>[modifier|default|modifier+choices] [value-notation]... [help-string]

Define a option.

# @option    --oa                   
# @option -b --ob                   short
# @option -c                        short only
# @option    --oc!                  required
# @option    --od*                  multiple
# @option    --oe+                  required + multiple
# @option    --ona <PATH>           value notation
# @option    --onb <FILE> <FILE>    multiple value notations
# @option    --oda=a                default
# @option    --odb=`_default_fn`    default from fn
# @option    --oca[a|b]             choice
# @option    --ocb[=a|b]            choice + default
# @option    --occ*[a|b]            multiple + choice
# @option    --ocd+[a|b]            required + multiple + choice
# @option    --ofa[`_choice_fn`]    choice from fn
# @option    --ofb[?`_choice_fn`]   choice from fn + no validation
# @option    --ofc*[`_choice_fn`]   multiple + choice from fn

@flag

@flag [short] <long>[*] [help string]

Define a flag. A flag is an option of boolean type, and is always false by default (e.g. --verbose, --quiet, --all, --long, etc).

# @flag     --fa 
# @flag  -b --fb         shoft
# @flag  -c              shoft only
# @flag     --fd*        multiple
# @flag  -e --fe*        short + multiple

@alias

@alias <name...>

Add aliases for subcommand.

# @cmd Run tests
# @alias t,tst
test() {
  echo Run test
}
USAGE: test.sh <COMMAND>

COMMANDS:
  test  Run tests [aliases: t, tst]

Meta

  • @describe: Sets the cli’s description.
  • @version: Sets cli's version.
  • @author: Sets cli's author.
# @describe A demo cli
# @version 2.17.1 
# @author nobody <[email protected]>

# @cmd Run test
test() {
  echo Run test
}
test.sh 2.17.1
nobody <[email protected]>
A demo cli

USAGE: test.sh <COMMAND>

COMMANDS:
  test  Run test

Value Notation

Value notation is used to describe value type of options and positional parameters.

# @option --target <FILE>
# @arg target <FILE>

Here are some value notation that will affect the shell completion.

  • FILE/PATH: complete files
  • DIR: complete directories

Shell Completion

Argc provides shell completion for argc command and all the bash scripts powered by argc.

# bash (~/.bashrc)
source <(argc --argc-completions bash mycmd1 mycmd2)

# elvish (~/.config/elvish/rc.elv)
eval (argc --argc-completions elvish mycmd1 mycmd2 | slurp)

# fish (~/.config/fish/config.fish)
argc --argc-completions fish mycmd1 mycmd2 | source

# nushell (~/.config/nushell/config.nu)
argc --argc-completions nushell mycmd1 mycmd2 # update config.nu manually according to output

# powershell ($PROFILE)
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
argc --argc-completions powershell mycmd1 mycmd2 | Out-String | Invoke-Expression

# xonsh (~/.config/xonsh/rc.xsh)
exec($(argc --argc-completions xonsh mycmd1 mycmd2))

# zsh (~/.zshrc)
source <(argc --argc-completions zsh mycmd1 mycmd2)

Replace mycmd1 mycmd2 with your argc scripts.

Argc can be used as multiple shell completion engine. see argc-completions

Argcscript

Argc will automatically find and run Argcfile.sh unless --argc-* options are used to change this behaviour.

Argcfile is to argc what Makefile is to make.

what is the benefit?

  • Can enjoy a handy shell completion.
  • Can be invoked in arbitrarily subdirectory, no need to locate script file each time.
  • As a centralized entrypoint/document for executing the project's bash scripts.
  • Serves as a script for a task runner.

You can use argc --argc-create to quickly create boilerplate Argcscripts. For example:

argc --argc-create test build run

The above command will create an Argcfile.sh in the current directory containing the commands: test, build and run.

Parallel

argc provides features for running commands/functions in parallel.

argc --argc-parallel "$0" cmd1 arg1 arg2 ::: cmd2

The above command will run cmd1 arg1 arg2 and cmd2 in parellel. Functions running in parallel mode can still access the argc_* variable.

Windows Only

Argc requires bash to run scripts. git's built-in bash is good enough for argc.

If you want to use another bash, please specify it via ARGC_SHELL_PATH environment variable.

If you want to run the bash script directly, you can add the following configuration to Windows Registry.

# Add .sh to PATHEXT
[Environment]::SetEnvironmentVariable("PATHEXT", [Environment]::GetEnvironmentVariable("PATHEXT", "Machine") + ";.SH", "Machine")
# Associate the .sh file extension with Git Bash
New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\sh_auto_file\shell\open\command' `
  -Name '(default)' -Value '"C:\Program Files\Git\bin\bash.exe" "%1" %*' -PropertyType String -Force

License

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.

More Repositories

1

dufs

A file server that supports static serving, uploading, searching, accessing control, webdav...
Rust
5,158
star
2

aichat

All-in-one AI CLI tool that integrates 20+ AI platforms, including OpenAI, Azure-OpenAI, Gemini, Claude, Mistral, Cohere, VertexAI, Bedrock, Ollama, Ernie, Qianwen, Deepseek...
Rust
3,111
star
3

window-switcher

Easily switch between windows of the same app with Alt+` (Backtick), also switch between apps with Alt+Tab.
Rust
415
star
4

upt

Universal Package-management Tool for any OS.
Rust
391
star
5

proxyfor

A lightweight proxy for capturing HTTP(S) and WS(S) traffic.
Rust
258
star
6

argc-completions

{bash,zsh,fish,powershell,nushell}-completions for 1000+ commands.
Shell
188
star
7

netease-music-crx

浏览器插件版网易云音乐
JavaScript
156
star
8

wechatpay

微信支付 SDK,支持刷卡支付、公众号支付、扫码支付、APP支付、H5支付,以及优惠券,红包,企业付款,微信代扣
TypeScript
113
star
9

apitest

Apitest is declarative api testing tool with JSON-like DSL.
TypeScript
110
star
10

projclean

Project dependencies & build artifacts cleanup tool.
Rust
92
star
11

htte

Document Driven API Test Framework
JavaScript
73
star
12

clii

Easily build a cli app.
TypeScript
40
star
13

dynimgen

A dynamic image generator.
Rust
30
star
14

opscan

A open port scanner.
Rust
30
star
15

chrome-extensions-manager

A snapshot based chrome extensions manager
JavaScript
27
star
16

wechat-devtools

使用 Linux, Docker 运行微信 web 开发者工具
Shell
21
star
17

llm-functions

Extend LLM with functions written in bash/js/python.
Shell
17
star
18

runme

[Deprecatd] A shell-script based task runner.
Rust
13
star
19

tomato-timer

A terminal tomato timer with notification
Rust
11
star
20

vmprotect-keygen

vmprotect keygen for nodejs
TypeScript
9
star
21

wasm-pkg-build

Effortlessly create npm packages from Rust wasm crates.
TypeScript
8
star
22

webhook

webhook-cli is a lightweight configurable tool written in NodeJS, that allows you to easily create HTTP endpoints (hooks) on your server, which you can use to execute configured commands
JavaScript
6
star
23

a-captcha

A Lightweight Pure JavaScript Captcha for Node.js
TypeScript
5
star
24

subexpo

Block explorer for Substrate based chain
JavaScript
4
star
25

xf

File-aware dynamic command runner.
Rust
3
star
26

node-fisheye

A opencv fisheye camera model bindings for Node.js.
C++
3
star
27

chatgpt-wechat-browser-extension

ChatGPT For Wechat FileHelper
JavaScript
2
star
28

glob-convert-encoding

Convert encoding of files that match glob
JavaScript
2
star
29

sequelize-modelgen

Generate sequelize models from sql
JavaScript
2
star
30

node-imagediff

Diff image to check whether objects have changed.
C++
2
star
31

gatsby-plugin-baidu-tongji

添加百度统计到 Gatsby 站点
JavaScript
2
star
32

deepin-wine-baidupan-arch

在Archlinux及衍生发行版上运行百度盘
Shell
2
star
33

conditions-lang

Boolean language for conditional builds, stages, jobs
TypeScript
2
star
34

fakepty

Run a command in a fake pty.
Rust
2
star
35

mynotes

My persional notebook.
2
star
36

orgdo

Command-line tool to manage the Todo lists
TypeScript
1
star
37

iredismodule

Create redis module with rust
Rust
1
star
38

trisue

Trisue是一款 REST API 调试及异常报告工具
JavaScript
1
star
39

dee

document-driven web framework, powered by express and openapi
TypeScript
1
star
40

use-services-packages

TypeScript
1
star
41

solid-color-page

Solid color page
HTML
1
star
42

fswebcam

wrap linux tool fswebcam to manipulate cameras
JavaScript
1
star
43

wasm-toml-js

TOML format for Node.js
Rust
1
star
44

alipay

蚂蚁金服开放平台 node sdk
TypeScript
1
star
45

myblog

Shell
1
star
46

toy-ipfs

Rust
1
star
47

change-case

Transform a string between camelCase, PascalCase, Capital Case, snake_case, param-case, CONSTANT_CASE and others.
Rust
1
star
48

mycrypt

Encrypt/decrypt your file
Rust
1
star
49

terminal-fonts

Big fonts for terminal display. Each character is a block formed by many dots.
Rust
1
star
50

calc-rs

A simple arithmetic calculator written in rust
Rust
1
star
51

install-gh-release

A website to automatically generate bash script to install binary published on github releases.
HTML
1
star
52

fp-course

Functional Programming Course
Haskell
1
star
53

dee-swaggerize

swagger document driven route builder for dee framework
JavaScript
1
star
54

install-binary

GitHub Action to install a binary from a GitHub Release
TypeScript
1
star