diff --git a/examples/README.md b/examples/README.md index 0ebf81e..9bb5e70 100644 --- a/examples/README.md +++ b/examples/README.md @@ -6,6 +6,7 @@ Each of these examples demonstrates one aspect or feature of argc. - [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. +- [strict.sh](./strict.sh) - how to use strict mode - [parallel.sh](./parallel.sh) - how to use `--argc-parallel`. - [args.sh](./args.sh) - all kinds of `@arg`. diff --git a/examples/strict.sh b/examples/strict.sh new file mode 100755 index 0000000..aa0d05d --- /dev/null +++ b/examples/strict.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +set -eu + + +# @flag --fa +# @option --oa +# @option --of*, multi-occurs + comma-separated list +# @option --oda=a default +# @option --oca[a|b] choice +# @option --ofa[`_choice_fn`] choice from fn +# @option --oxa~ capture all remaining args + +main() { + ( set -o posix ; set ) | grep ^argc_ + echo "${argc__fn:-}" "$@" +} + +_choice_fn() { + echo abc + echo def + echo ghi +} + +eval "$(argc --argc-eval "$0" "$@")" \ No newline at end of file diff --git a/src/build.rs b/src/build.rs index 600a2ea..05b11d2 100644 --- a/src/build.rs +++ b/src/build.rs @@ -13,7 +13,7 @@ const UTIL_FNS: [(&str, &str); 6] = [ _argc_take_args() { _argc_take_args_values=() _argc_take_args_len=0 - local param="${1:-}" min="${2:-}" max="${3:-}" signs="${4:-}" delimiter="${5:-}" + local param="$1" min="$2" max="$3" signs="$4" delimiter="$5" if [[ "$min" -eq 0 ]] && [[ "$max" -eq 0 ]]; then return fi @@ -140,7 +140,7 @@ _argc_require_params() { r#" _argc_validate_choices() { local render_name="$1" raw_choices="$2" choices item choice concated_choices="" - IFS=$'\n' read -d '' -r -a choices <<<"$raw_choices" + IFS=$'\n' read -r -a choices <<<"$raw_choices" for choice in "${choices[@]}"; do if [[ -z "$concated_choices" ]]; then concated_choices="$choice" diff --git a/tests/cli.rs b/tests/cli.rs index a219a80..ea6bf3f 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -98,10 +98,10 @@ fn build_stdout() { } #[test] -fn build_outpath() { - let path = locate_script("examples/demo.sh"); +fn run_build() { + let path = locate_script("examples/strict.sh"); let tmpdir = tmpdir(); - let outpath = tmpdir.join("demo.sh"); + let outpath = tmpdir.join("strict.sh"); Command::cargo_bin("argc") .unwrap() .arg("--argc-build") @@ -109,8 +109,23 @@ fn build_outpath() { .arg(&outpath) .assert() .success(); - let script = std::fs::read_to_string(&outpath).unwrap(); - assert!(script.contains("# ARGC-BUILD")); + + Command::cargo_bin("argc") + .unwrap() + .arg("--argc-run") + .arg(&outpath) + .args([ + "--fa", + "--oa", + "oa1", + "--of=of1,of2", + "--oca=a", + "--ofa", + "abc", + ]) + .assert() + .stdout(predicates::str::contains("argc__fn=main")) + .success(); } #[test] diff --git a/tests/fixtures.rs b/tests/fixtures.rs index 77a5f1f..129e825 100644 --- a/tests/fixtures.rs +++ b/tests/fixtures.rs @@ -116,7 +116,6 @@ pub fn run_script>( let envs: HashMap<&str, &str> = envs.iter().cloned().collect(); let shell_path = argc::NativeRuntime.shell_path().unwrap(); let output = std::process::Command::new(shell_path) - .arg("-u") .arg(script_path.as_ref()) .args(args) .env("PATH", path_env_var.clone()) diff --git a/tests/validate.rs b/tests/validate.rs index 53eaefb..1206c43 100644 --- a/tests/validate.rs +++ b/tests/validate.rs @@ -249,7 +249,7 @@ fn choice_access_vars() { # @flag --fa # @arg val[`_choice_fn`] _choice_fn() { - if [[ ${argc_fa:-} == 1 ]]; then + if [[ $argc_fa == 1 ]]; then echo abc else echo def @@ -266,10 +266,10 @@ fn choice_slash() { # @arg foo # @arg bar[`_choice_fn`] cmd() { - echo ${1:-} + echo $1 } _choice_fn() { - echo ${1:-} + echo $1 } "###; snapshot_multi!(script, [vec!["prog", "cmd", "a\\b", "a\\b"],]);