Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pbrisbin/yago
A toy for seeing the most complicated way to parse options in bash
https://github.com/pbrisbin/yago
Last synced: about 2 months ago
JSON representation
A toy for seeing the most complicated way to parse options in bash
- Host: GitHub
- URL: https://github.com/pbrisbin/yago
- Owner: pbrisbin
- Created: 2011-06-14T21:32:57.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2011-07-08T20:58:46.000Z (over 13 years ago)
- Last Synced: 2024-10-09T09:45:24.108Z (3 months ago)
- Language: Shell
- Homepage:
- Size: 113 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Yago
Yet Another GetOpt. An experiment in the flexibility and limitations of
bash.### Description
`yago` intends to be an easy way for bash scripts to parse command line
options without the usual `while`/`case` staircase.There are other tools that do this (which have inspired it), but I
wanted to see if it could be done in pure bash.The main features are its terse usage, automatic validation, and an
automatic help message.### Usage
~~~ { .bash }
#!/bin/bashsource ./yago
yago_parse 'testprog' "$@" << EOF
quiet, q, output less crap
files, f, list of files to process , REQUIRED, N, FILE
size , s, desired output size\, ok?, 150 , 1, SIZE, n_sizeEOF
# continue your script knowing that options have been parsed and the
# variables $quiet, $files[], and $size[] are set correctly. unprocessed
# args are placed in $args[].echo "quiet is $quiet"
echo "size is ${size[@]}"
echo "files are ${files[@]}"
echo "leftover args are ${args[@]}"exit 0
~~~Note: the `quiet` option has the minimum required fields to be a valid
definition and `size` has all available attributes utilized.~~~
$ ./example.sh -h
usage: testprog [ -q ] -f FILE ... [ -s SIZE ]
-q, --quiet output less crap
-f, --files FILE ... list of files to process
-s, --size SIZE desired output size, ok?
~~~~~~
$ ./example.sh
error: option --files is required
~~~~~~
$ ./example.sh -x
error: invalid option -x
~~~~~~
$ ./example.sh -q --quiet
error: duplicate option --quiet
~~~~~~
$ ./example.sh --files foo
quiet is false
size is 150
files are foo
leftover args are
~~~~~~
$ ./example.sh -q -s 10 12 -f a b c
error: extra argument for --size
~~~~~~
$ ./example.sh -q -s --files a b c
error: missing argument for --size
~~~~~~
$ ./example.sh -q -s 10 -f foo bar -- baz bat
quiet is true
size is 10
files are foo bar
leftover args are baz bat
~~~### TODO
* implement custom variable