- This is supposed to be a basic parser of command line arguments for shell scripts
- The idea is that you give it a spec in a certain format and command line arguments that were inputted to the script and it parses them
by turning them into simple
variablename=value - This makes it so that you can just call it with
eval "$(echo "$spec" | clparser -- "$@")"-
This way it will call the
variablename=valueand you can go on without having to worry about positioning or anything -
where
spec='flags: f,flag g qwerty h,hist; parameters: q,asdf u nothing zzz,z;'and prints out
flag=false
f=false
h=false
help=false
g=false
qwerty=true
asdf="${2}"
q="${2}"
Z='test'
zzz='test'
z='test'
u='defval'
nothing="${8}"
set -- "${4}" "${6}" "${10}" "${11}"if called with arguments -fgq whatever --qwerty 'i dunno' --zzz=test words --nothing else -- --test hi
which results in
f,flag = false
g = false
qwerty = true
h,help = false
q,asdf = whatever
u = defval
nothing = else
zzz,z,Z = test
positional args: i dunno words --test hi
-
example scripts have been added showing off the features
- try running the scripts to see the output and how it can be used
- for
zshandksh, see theexample.bashscript, it uses the same syntax - for
xonsh, probably just use argparse, its built in and very good
-
if
clparsersees--in the args, it will stop processing the args and just output everything after as positional args- this is used to let
clparserknow all the remaining args should be passed through - this is how clparser knows the difference between it's own args to parse and the scripts args
- any args not starting with - and not after a parameter will be turned into a positional arg
- this is used to let
-
clparseruses direct references to the input positional arguments as much as possible to avoid issues with whitespace or other symbol expansion -
flags are returned as true or false
- use in
shlike"$force" && rm -f, running thetrueandfalsecommands rather than testing the string
- use in
if "$force"; then
echo force
fibash,zsh,ksh, andxonshuse associative arrays for flags and params (see example.* scripts for examples)sh,csh, andfishall use plain variables because they don't have associative arrays and variables are sufficient
bash,zsh,ksh,xonsh,fish, andcshuse arrays for the positional argumentsshwill override argv to it can preserve whitespace in the input arguments--override-argvwill force overriding argv in all shells--maintain-argvwill force maintaining argv in all shells
- the shell syntax language is determined by the calling process
- NOTE: this doesn't seem to work for xonsh, at least while installed through the Appimage
--shellspecifies a specific shell to use
clparser --helpwill show all the options- NOTE: currently
--help-msgis unimplemented
- NOTE: currently
clparserwas written so it can be used as a C libclparseruses the library as intended for its own arguments
#include "process.h"and add process.o and map.o to the compilation command