Skip to main content

Argparse for shell scripts

Project description

argparsh - python argparse for the shell

Ever wanted to parse arguments in bash but felt frustrated by giant case blocks and unfriendly syntax? Ever tried getopts but ended up curled on the floor sobbing? Have you ever spent sleepless nights hoping that bash argument parsing could be as simple as python's argparse? Maybe argparsh is for you.

argparsh aims to provide an easy way to construct an argument parsing program from any shell.

Usage

# Create a parser that accepts a string and an optional int value
parser=$({
    # Initialize the parser with the name of the script and a description
    argparsh new $0 -d "Example parser"

    # Add a positional argument - note that args after -- are options to add_arg
    # and not aliases for the argument
    argparsh add_arg strarg -- --help "My string argument"

    # Add a keyword argument that can either be -i <value> or --intarg <value>
    argparsh add_arg -i --intarg -- \
        --help "My int argument" \
        --type int \
        --default 10
})

# Parse the input arguments with the parser above
eval $(argparsh parse $parser -- "$@")

# Access parsed arguments by name
echo "String argument was" $strarg
echo "Integer argument was" $intarg

See example.sh for a more complete example, and examples of alternate output formats (e.g. parsing CLI arguments into an associative array)

Installation

No dependencies besides python/pip.

pip install argparsh

Or, to build from source:

git clone https://github.com/aneeshdurg/argparsh.git
cd argparsh
pip install .

Similar Works

argparsh differs from previous attempts at improving shell argument parsing by being shell agnostic (although only bash is tested, it is relatively easy to add support for more shells, contributions are welcome), supporting subcommands/subparsers, and allowing detailed + customizable help text to be configured.

  • getopts
    • the OG argument handling utility. A bit clunky to use.
  • argparse.sh
    • similar approach, but not completely shell agnostic, and lacking in more advanced features, like subcommands.
  • fish argparse
    • Fish specific, but a nice utility. Not easy to use outside of fish and no subcommand support.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

argparsh-0.0.4.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

argparsh-0.0.4-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file argparsh-0.0.4.tar.gz.

File metadata

  • Download URL: argparsh-0.0.4.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for argparsh-0.0.4.tar.gz
Algorithm Hash digest
SHA256 13f571eaa9522d1e2425e7d596a51be038f26cb393f7360faaae206a3e0d8b5f
MD5 03c02ff03c6d8a345aaa6dd1a4351021
BLAKE2b-256 488171e78fd2aa05391474286bc579663925e99d104b8e6ec73aca99882f1151

See more details on using hashes here.

File details

Details for the file argparsh-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: argparsh-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for argparsh-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4e938f413e6de50490b435e3cd91b3e2662c0277a5faab58e4b1402e0ad12506
MD5 da10623dfe7705dff38e30421b62eee6
BLAKE2b-256 e79c986d190c78af4882c41a484cf9131624155dd875b7bda6b99b4ae2795a69

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page