Skip to main content

pyparam

pypi pypi codacy quality codacy quality docs github action pyver

Powerful parameter processing

Features

  • Command line argument parser (with subcommand support)
  • Rich type support, including py, json, namespace, etc.
  • Type overwriting for parameters from command line
  • Arbitrary parsing arguments from command line
  • Automatic help page assembling
  • Help page customization
  • Callbacks for option values
  • Parameter loading from configuration files

Installation

pip install -U pyparam

Documentation

https://pwwang.github.io/pyparam/

Basic usage

example.py

from rich import print
from pyparam import Params
# program name, otherwise sys.argv[0]
params = Params(prog='pyparam', desc="An example for %(prog)s")
# adding parameters
params.add_param('i, int', type=int,
                 desc="An integer argument.")
params.add_param('float', default=0.1, # type float implied
                 desc="A float argument.")
params.add_param('str', type=str,
                 desc="A str argument.")
params.add_param('flag', type=bool,
                 desc="A flag argument.")
params.add_param('c,count', type='count',
                 desc="A count argument.")
params.add_param('a', type='auto', type_frozen=False,
                 desc="Value will be automatically casted.")
params.add_param('py', type='py',
                 desc="Value will be evaluated by `ast.literal_eval`.")
params.add_param('json', type='json',
                 desc="Value will be converted using `json.loads`.")
params.add_param('list', type='list',
                 desc="Values will be accumulated.")
params.add_param('path', type='path', required=True,
                 desc="Value will be casted into `pathlib.Path`.",
                 callback=( # check if path exists
                     lambda path: ValueError('File does not exist.')
                     if not path.exists() else path
                 ))
params.add_param('choice', type='choice', default='medium',
                 choices=['small', 'medium', 'large'],
                 desc="One of {choices}.")
params.add_param('config.ncores', default=1, # namespace config implied
                 argname_shorten=False,
                 desc='Number of cores to use.')

print(vars(params.parse()))

Try it out:

python example.py

help

$ python example.py \
    -i2 \
    --float 0.5 \
    --str abc \
    -ccc \
    -a:int 1 \
    --py "{1,2,3}" \
    --json "{\"a\": 1}" \
    --list 1 2 3 \
    --choice large \
    --path . \
    --config.ncores 4
{
    'i': 2,
    'int': 2,
    'float': 0.5,
    'str': 'abc',
    'flag': False,
    'c': 3,
    'count': 3,
    'a': 1,
    'py': {1, 2, 3},
    'json': {'a': 1},
    'list': [1, 2, 3],
    'path': PosixPath('.'),
    'choice': 'large',
    'config': Namespace(ncores=4)
}

Try more features with:

python -m pyparam

Shell completions

Here is how the command completion in fish works:

pyparam-completions

Check the documentation, as well as the __main__.py to see how the completion works.

Metadata

Release files for pyparam 0.5.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyparam 0.5.4
File Size Uploaded
pyparam-0.5.4.tar.gz 39.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyparam 0.5.4
File Interpreter ABI Platform
pyparam-0.5.4-py3-none-any.whl Python 3 none any Details

Total release size: 81.4 kB

Release files / pyparam-0.5.4.tar.gz

Download URL pyparam-0.5.4.tar.gz
Size 39.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5513742f9668429d42e7b0acaf02c6ae868466b048ff91f1241f0c6d067a0af5
BLAKE2b-256 checksum
How to use checksums
46c84d326f8a256ac668e69f21caa53c56bb54905603f8af8f067c6237164ae2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-1031-azure

Release files / pyparam-0.5.4-py3-none-any.whl

Download URL pyparam-0.5.4-py3-none-any.whl
Size 41.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fc94f0aa4914274dcf07c8ae270470ef63b36efd31bc2a38ba22ab00de35a14
BLAKE2b-256 checksum
How to use checksums
efa9c159317de8b5ddb57bc01712e2e5231be7a04e2aa449778596c10cb769e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-1031-azure

Release history Release notifications | RSS feed

This release

0.5.4 This release

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.17

2 release files

0.4.16

2 release files

0.4.13

2 release files

0.4.12

2 release files

0.4.11

2 release files

0.4.10

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page