Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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/bash

source ./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_size

EOF

# 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