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)

TODO

  • Support for more output formats (fish, JSON, ...)

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

  • 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.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

argparsh-0.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: argparsh-0.0.3.tar.gz
  • Upload date:
  • Size: 5.8 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.3.tar.gz
Algorithm Hash digest
SHA256 3c860c6525a444b681757d5d50c2e32dfbc602db95de4bd95d17c60b2c19b26c
MD5 6e4bcf1de075ac3593f161c337f0e866
BLAKE2b-256 19653e10690dd33439c17a18a9f46a5749473c54ba6cc9ffa26dc62ae012c2ae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: argparsh-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 63940598719bb0afaa84a5da587ca5f209fe86fb5e3ce7e0d3b2325c18dad18a
MD5 8f7175f1bbe3e957fb4b80fcfefc2c69
BLAKE2b-256 7a614a8b4d28fcb657ce54de422786f84e040da1190d2c21144aa347a13b7a59

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