Skip to content

Latest commit

 

History

History
180 lines (118 loc) · 2.43 KB

README.md

File metadata and controls

180 lines (118 loc) · 2.43 KB

Catch All with stdin input

Demonstrates how to process multiple files as arguments, or from stdin.

This example was generated with:

$ bashly init
# ... now edit src/bashly.yml to match the example ...
# ... now edit src/root_command.yml to match the example ...
$ bashly generate

bashly.yml

name: cli
help: Sample application
version: 0.1.0

catch_all:
  label: file
  help: Path to one or more files. Reads from stdin if empty or "-".

flags:
- long: --format
  short: -f
  arg: format
  help: Specify file format
  default: json
  allowed: [csv, json]

examples:
- cli file1 file2 --format csv
- cli --format csv file1 file2
- cat file1 | cli --format csv
- cat file* | cli - --format csv

src/root_command.sh

inspect_args

# Read contents of the provided file(s)
content=""
for file in "${other_args[@]}"; do
  content+="$(cat "$file")"
  content+=$'\n'
done

# Read stdin if file(s) not provided as arguments
if [[ -z "$content" ]]; then
  content="$(cat -)"
fi

echo
echo "collected file contents:"
echo "$content"
echo

Output

$ ./cli -h

cli - Sample application

Usage:
  cli [OPTIONS] [--] [FILE...]
  cli --help | -h
  cli --version | -v

Options:
  --format, -f FORMAT
    Specify file format
    Allowed: csv, json
    Default: json

  --help, -h
    Show this help

  --version, -v
    Show version number

Arguments:
  FILE...
    Path to one or more files. Reads from stdin if empty or "-".

Examples:
  cli file1 file2 --format csv
  cli --format csv file1 file2
  cat file1 | cli --format csv
  cat file* | cli - --format csv


$ ./cli file1 file2 --format csv

args:
- ${args[--format]} = csv

other_args:
- ${other_args[*]} = file1 file2
- ${other_args[0]} = file1
- ${other_args[1]} = file2

collected file contents:
file1 content
file2 content



$ ./cli -f=csv file1 file2

args:
- ${args[--format]} = csv

other_args:
- ${other_args[*]} = file1 file2
- ${other_args[0]} = file1
- ${other_args[1]} = file2

collected file contents:
file1 content
file2 content



$ cat file1 | ./cli --format csv

args:
- ${args[--format]} = csv

collected file contents:
file1 content


$ cat file* | ./cli -

args:
- ${args[--format]} = json

other_args:
- ${other_args[*]} = -
- ${other_args[0]} = -

collected file contents:
file1 content
file2 content