Skip to content

de9uch1/argparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

argparser: argument parser for shell script

INSTALLATION

I recommend to use pipx to install argparser.

pipx install git+https://github.com/de9uch1/argparser.git

Also you can install by pip.

pip install git+https://github.com/de9uch1/argparser.git

USAGE

  1. Define add_args() function and add arguments.
  2. The first argument of argparser add is a variable name in the script which will be set to the given argument value.
  3. The second positional argument, --long/-l and --short/-s options will be command line argument names.
  4. Other arguments of argparser add can be shown by argparser add --help.
  5. eval $(add_args | argparser parse "$@") parses command line arguments.

The parser is based on argparse.ArgumentParser from Python.

EAMPLE

#!/bin/bash

function add_args() {
    argparser setup $0 "Test script."
    argparser add FILE        file
    argparser add WORKERS  -l num-workers  -s n --type int --default 8
    argparser add USER_IDS -l user-ids     -s u --type int --nargs "*"
    argparser add BETA     -l experimental      --action store_true
    argparser add LANGUAGE -l lang              --choices en de ja
}
eval $(add_args | argparser parse "$@")

echo $FILE
echo $WORKERS
echo ${USER_IDS[@]}
$BETA && echo T || echo F
echo $LANGUAGE
$ ./script.sh log.txt --num-workers 16 -u 100 200 --experimental --lang ja

The variables in the script will be set to:

  • FILE=log.txt
  • WORKERS=16
  • USER_IDS=(100 200) # Stored in array
  • BETA=true # This is helpful for such situation: if $BETA; then ...
  • LANGUAGE=ja

You can also see the help messages of the defined arguments by ./script.sh --help.

NOTE

This software was inspired by https://github.com/ko1nksm/getoptions .

About

Argument parser for shell script

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages