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.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: argparsh-0.0.2.tar.gz
  • Upload date:
  • Size: 5.9 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.2.tar.gz
Algorithm Hash digest
SHA256 6e8acba2de6b6f8d9c584ac97dc55fa1e81ebb493035f1c5cee70f184ae003f7
MD5 b5eff60ffbd631207bea38155e63fd88
BLAKE2b-256 4f16e17116ebc0dae676cc0bb2a2118597cb7ff8374d0547c2345fced1d529b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: argparsh-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 41882cddc02be2186ddeb1af81a004bcbef2004fc416150130c63e7bb04ead69
MD5 bbfc41e78af00fe469cdf4e941cfe791
BLAKE2b-256 228b3280cbb49ec109dd89249ba3daa25088017e4067317083ce52597e003649

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