Skip to content

Commit

Permalink
refactor: update examples and add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Apr 16, 2024
1 parent cef2831 commit 76b5cfd
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 71 deletions.
2 changes: 1 addition & 1 deletion docs/command-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Argc is a also command runner built for those who love the efficiency and flexibility of Bash scripting.

This document explains how to effectively use `argc` as a command runner.
This guide provides instructions on how to effectively use `argc` for this purpose.

## Create an Argcfile.sh

Expand Down
20 changes: 20 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Argc Examples

Each of these examples demonstrates one aspect or feature of argc.

- [demo.sh](./demo.sh) - A simple demo script.
- [multiline.sh](./multiline.sh) - how to use multiline help text.
- [nested-commands](./nested-commands.sh) - how to use nested commands.
- [hooks.sh](./hooks.sh) - how to use argc hooks.
- [parallel.sh](./parallel.sh) - how to use `--argc-parallel`.

- [args.sh](./args.sh) - all kinds of `@arg`.
- [options.sh](./options.sh) - all kinds of `@option` and `@flag`.
- [bind-env](./bind-envs.sh) - how to bind env to param.
- [envs.sh](./envs.sh) - all kind of `@env`.

- [default-subcommand](./default-subcommand.sh) - how to use `@meta default-subcommand`.
- [require-tools](./require-tools.sh) - how to use `@meta require-tools`.
- [inherit-flag-options](./inherit-flag-options.sh) - how to use `@meta inherit-flag-options`.
- [combine-short](./combine-shorts.sh) - how to use `@meta combine-shorts`.
- [symbol](./symbol.sh): how to use `@meta symbol`.
Empty file modified examples/args.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion examples/bind-envs.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @cmd Demonstrates how to bind environment variables to parameters
# @cmd How to bind env to param

# @flag --fa1 $$
# @flag --fa2 $$
Expand Down
19 changes: 19 additions & 0 deletions examples/combine-shorts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @describe How to use `@meta combine-shorts`
#
# Mock rm cli
# Examples:
# prog -rf dir1 dir2
#
# @meta combine-shorts
# @flag -r --recursive remove directories and their contents recursively
# @flag -f --force ignore nonexistent files and arguments, never prompt
# @arg path* the path to remove

eval "$(argc --argc-eval "$0" "$@")"

_debug() {
( set -o posix ; set ) | grep ^argc_
echo "$argc__fn" "$@"
}

_debug
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# describe Sample CLI that uses the default command option
# describe How to use `@meta default-subcommand`

# @cmd Upload a file
# @meta default-subcommand
Expand Down
Empty file modified examples/envs.sh
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions examples/hooks.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#/usr/bin/env node
set -e

# @describe How to use argc hooks
#
# Argc supports two hooks:
# _argc_before: call before running the command function (after initialized variables)
# _argc_after: call after running the command function

set -e

_argc_before() {
echo before
}
Expand Down
26 changes: 26 additions & 0 deletions examples/inherit-flag-options.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @describe How to use `@meta inherit-flag-options`
#
# Mock systemctl cli
# Examples:
# prog --user start my-service
# prog --user stop my-service
#
# @meta inherit-flag-options
# @flag --user Connect to user service manager
# @flag --no-pager Do not pipe output into a pager
# @option -t --type List units of a particular type
# @option --state List units with particular LOAD or SUB or ACTIVE state

# @cmd Start (activate) one or more units
# @arg UNIT... The unit files to start
start() {
:;
}

# @cmd Stop (deactivate) one or more units
# @arg UNIT... The unit files to stop
stop() {
:;
}

eval "$(argc --argc-eval "$0" "$@")"
15 changes: 8 additions & 7 deletions examples/details.sh → examples/multiline.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# @describe Multi-line auto-wrapped help text
# @describe How to use multiline help text
#
# Extra lines after the @cmd or @describe, which don't start with an @, are
# treated as the long description. A line which is not a comment ends
# the block.
# Extra lines after the comment tag accepts description, which don't start with an `@`,
# are treated as the long description. A line which is not a comment ends the block.

# @meta version 1.0.0
# @meta author nobody <[email protected]>

# @option --foo[=default|full|auto] Sunshine gleams over hills afar, bringing warmth and hope to every soul, yet challenges await as we journey forth, striving for dreams and joy in abundance. Peaceful rivers whisper secrets gently heard.
# * default: enables recommended style components.
Expand All @@ -13,9 +15,8 @@
# Use '-' for standard input.
# @cmd Eager dogs jump quickly over lazy foxes, creating wonderful chaos amid peaceful fields, but few noticed their swift escape beyond tall fences. Swift breezes sway gently through green.
#
# Extra lines after the @cmd or @describe, which don't start with an @, are
# treated as the long description. A line which is not a comment ends
# the block.
# Extra lines after the comment tag accepts description, which don't start with an `@`,
# are treated as the long description. A line which is not a comment ends the block.
cmd() { :; }

eval "$(TERM_WIDTH=`tput cols` argc --argc-eval "$0" "$@")"
4 changes: 3 additions & 1 deletion examples/command-nested.sh → examples/nested-commands.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# @describe Docker CLI mocking
# @describe How to use nested subcommands
#
# Mock docker cli

# @cmd
builder() { :; }
Expand Down
3 changes: 2 additions & 1 deletion examples/parallel.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#/usr/bin/env bash
set -e

# @describe Demonstrates how to use argc-parallel
# @describe How to use `--argc-parallel`
#
# Compared with GNU parallel, the biggest advantage of argc-parallel is that it preserves `argc_*` variables.

# @cmd
Expand Down
8 changes: 4 additions & 4 deletions examples/require-tools.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# @describe Demonstrate how to use require-tools meta
# @describe how to use `@meta require-tools`

# @meta require-tools curl,sed
# @meta require-tools awk,sed

# @cmd
# @meta require-tools git
cmd1() {
require-git() {
:;
}

# @cmd
# @meta require-tools not-found
cmd2() {
require-not-found() {
:;
}

Expand Down
26 changes: 26 additions & 0 deletions examples/symbol.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @describe How to use `@meta symbol`
#
# Mock cargo cli
# @meta symbol +toolchain[`_choice_toolchain`]

# @cmd Compile the current package
# @alias b
build () {
:;
}

# @cmd Analyze the current package and report errors, but don't build object files
# @alias c
check() {
:;
}

_choice_toolchain() {
cat <<-'EOF'
stable
beta
nightly
EOF
}

eval "$(argc --argc-eval "$0" "$@")"
16 changes: 0 additions & 16 deletions tests/details.rs

This file was deleted.

16 changes: 16 additions & 0 deletions tests/multiline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::SCRIPT_MULTILINE;

#[test]
fn wrap() {
snapshot!(SCRIPT_MULTILINE, &["prog", "-h"], None, Some(80));
}

#[test]
fn wrap2() {
snapshot!(SCRIPT_MULTILINE, &["prog", "foo", "-h"], None, Some(80));
}

#[test]
fn nowrap() {
snapshot!(SCRIPT_MULTILINE, &["prog", "-h"], None, None);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: tests/bind_env.rs
expression: "format!(r#\"\n# OUTPUT\n{output}\n\n# BUILD_OUTPUT\n{build_output}\n\"#)"
---
# OUTPUT
Demonstrates how to bind environment variables to parameters
How to bind env to param

USAGE: bind-envs flags [OPTIONS]

Expand All @@ -18,7 +18,7 @@ OPTIONS:


# BUILD_OUTPUT
Demonstrates how to bind environment variables to parameters
How to bind env to param

USAGE: prog flags [OPTIONS]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
---
source: tests/details.rs
source: tests/multiline.rs
expression: data
---
RUN
prog -h

# OUTPUT
command cat >&2 <<-'EOF'
Multi-line auto-wrapped help text
prog 1.0.0
nobody <nobody@example.com>
How to use multiline help text

Extra lines after the @cmd or @describe, which don't start with an @, are
treated as the long description. A line which is not a comment ends
the block.
Extra lines after the comment tag accepts description, which don't start with an `@`,
are treated as the long description. A line which is not a comment ends the block.

USAGE: prog [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -45,11 +46,12 @@ EOF
exit 0

# BUILD_OUTPUT
Multi-line auto-wrapped help text
prog 1.0.0
nobody <[email protected]>
How to use multiline help text

Extra lines after the @cmd or @describe, which don't start with an @, are
treated as the long description. A line which is not a comment ends
the block.
Extra lines after the comment tag accepts description, which don't start with an `@`,
are treated as the long description. A line which is not a comment ends the block.

USAGE: prog [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -78,5 +80,3 @@ OPTIONS:

COMMANDS:
cmd Eager dogs jump quickly over lazy foxes, creating wonderful chaos amid peaceful fields, but few noticed their swift escape beyond tall fences. Swift breezes sway gently through green.


Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
---
source: tests/details.rs
source: tests/multiline.rs
expression: data
---
RUN
prog -h

# OUTPUT
command cat >&2 <<-'EOF'
Multi-line auto-wrapped help text
prog 1.0.0
nobody <nobody@example.com>
How to use multiline help text

Extra lines after the @cmd or @describe, which don't start with an @, are
treated as the long description. A line which is not a comment ends
the block.
Extra lines after the comment tag accepts description, which don't start with
an `@`,
are treated as the long description. A line which is not a comment ends the
block.

USAGE: prog [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -53,11 +56,12 @@ EOF
exit 0

# BUILD_OUTPUT
Multi-line auto-wrapped help text
prog 1.0.0
nobody <[email protected]>
How to use multiline help text

Extra lines after the @cmd or @describe, which don't start with an @, are
treated as the long description. A line which is not a comment ends
the block.
Extra lines after the comment tag accepts description, which don't start with an `@`,
are treated as the long description. A line which is not a comment ends the block.

USAGE: prog [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -86,5 +90,3 @@ OPTIONS:

COMMANDS:
cmd Eager dogs jump quickly over lazy foxes, creating wonderful chaos amid peaceful fields, but few noticed their swift escape beyond tall fences. Swift breezes sway gently through green.


Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
---
source: tests/details.rs
source: tests/multiline.rs
expression: data
---
RUN
prog foo -h

# OUTPUT
command cat >&2 <<-'EOF'
Multi-line auto-wrapped help text
prog 1.0.0
nobody <nobody@example.com>
How to use multiline help text

Extra lines after the @cmd or @describe, which don't start with an @, are
treated as the long description. A line which is not a comment ends
the block.
Extra lines after the comment tag accepts description, which don't start with
an `@`,
are treated as the long description. A line which is not a comment ends the
block.

USAGE: prog [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -53,11 +56,12 @@ EOF
exit 0

# BUILD_OUTPUT
Multi-line auto-wrapped help text
prog 1.0.0
nobody <[email protected]>
How to use multiline help text

Extra lines after the @cmd or @describe, which don't start with an @, are
treated as the long description. A line which is not a comment ends
the block.
Extra lines after the comment tag accepts description, which don't start with an `@`,
are treated as the long description. A line which is not a comment ends the block.

USAGE: prog [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -86,5 +90,3 @@ OPTIONS:

COMMANDS:
cmd Eager dogs jump quickly over lazy foxes, creating wonderful chaos amid peaceful fields, but few noticed their swift escape beyond tall fences. Swift breezes sway gently through green.


Loading

0 comments on commit 76b5cfd

Please sign in to comment.