Skip to main content

Parse command line arguments into a list of args and a dict of kwargs.

Project description

pargv

Parse command line arguments into a list of args and a dict of kwargs.

Installation

pip install pargv

Usage

By default, pargv.parse_args() uses sys.argv as command line arguments. It can be used in the following way.

from pargv import parse_args

args, kwargs = parse_args()

The arguments to be parsed can also be specified manually:

from pargv import parse_args

args, kwargs = parse_args(argv=['pargv.py', '--name=pargv'])

Specification

parse_args parses arguments in the following way, assuming the following command line arguments (sys.argv): ['/pargv/pargv.py', 'command', 'positional', '--flag', '--optional=value', 'test', '--output-file', 'filename', '-ab=one', 'two']

By calling args, kwargs = parse_args(), this would return the following list and dict:

args = ['/pargv/pargv.py', 'command', 'positional']
kwargs = {
        'flag': True,
        'optional': ['value', 'test'],
        'output_file': 'filename',
        'a': True,
        'b': ['one', 'two'],
    }

Basic behaviour

  • All arguments before the first option (starting with a hyphen) are considered positional arguments (args)
  • All other arguments are considered optional keyword arguments (kwargs)
  • Optional arguments without leading hyphens are considered as values to preceding keyword arguments
  • One value of an option is stored as a string, multiple values are stored as a list
  • Flags are stored with the value True in the dict
  • Up to 2 leading hyphens are stripped from options, all other hyphens are converted into underscores (---test-this- would become the key _test_this_ in the dict)
  • For multiple short options such as -vao=file.ext or -vao file.ext, the last option (o) is set to the specified value, while other options (v and a) are set to True.

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

pargv-0.2.0.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pargv-0.2.0-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

File details

Details for the file pargv-0.2.0.tar.gz.

File metadata

  • Download URL: pargv-0.2.0.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.6 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.28.1 rfc3986/1.5.0 tqdm/4.64.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pargv-0.2.0.tar.gz
Algorithm Hash digest
SHA256 01237a76913cb6978b91f418110407b4e778746ceb37546a20e987b9b6759e14
MD5 1c44a10a80e0191b7c027db269b01832
BLAKE2b-256 fdecb3bb89c27aea49f21e0e7ec38100385311b50cf3a7b413e660f50a73a787

See more details on using hashes here.

File details

Details for the file pargv-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pargv-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 2.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.6 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.28.1 rfc3986/1.5.0 tqdm/4.64.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pargv-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 320c51026a22dd9b5a88bafb782a425bb5702277d04b1f4242df73ca15013dc4
MD5 6b0e78c3f99d22ef1f9a07d11e1f6ecf
BLAKE2b-256 ca0a57450159ab42a7ed9fc3b6b9e8756dcd54b214510bd2eff35ef5f7431fcd

See more details on using hashes here.

Supported by

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