Skip to main content

Python tests

argpass

An argparse extension to collect and pass on command line arguments (potentially starting with hyphens) to other programs.

TL;DR:

Imagine writing a wrapper script that calls two or more other programs. In many cases, you will want to collect command line arguments for these programs and pass them on unaltered. Using argparse, this is not possible. With argpass, simply pass nargs=argpass.NargsOption.COLLECT_UNTIL_NEXT_KNOWN to parser.add_argument and it will collect all strings (regardless of format) until the next known argument.

Installation

Install with

pip install argpass

Motivation

Built-in argparse lacks an option to ignore unrecognized flag strings (usually starting with - or --), which makes it difficult to collect arguments and pass them on to other programs in some cases. It can be done with ArgumentParser.parse_known_args (see docs) which collects all unrecognized arguments, but this only works when just one other program is invoked from our script. Collecting arguments starting with dashes to pass on to more than one program is impossible with argparse, which as caused quite some frustration.

An example

Consider the following example: We have a Python script that takes an input file, a regular argument, and other arguments that should be passed on to another program which is invoked from within our script. We would call our script as follows:

python script.py --file example.txt --regular-arg exampleArg --args-to-pass-on --param1 val1 --param2 val2

Thanks to ArgumentParser.parse_known_args, this case can be handled by argparse just fine:

$ cat script.py
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--file")
parser.add_argument("--regular-arg")
parser.add_argument("--args-to-pass-on", nargs="*")
args, unknown = parser.parse_known_args()
args.args_to_pass_on = unknown
print(args)

$ python script.py --file example.txt --regular-arg exampleArg --args-to-pass-on --param1 val1 --param2 val2
Namespace(file='example.txt', regular_arg='exampleArg', args_to_pass_on=['--param1', 'val1', '--param2', 'val2'])

However, this approach only works when we want to collect arguments for just a single program. Doing something like

python script.py \
    --file example.txt --regular-arg exampleArg \
    --args-to-pass-on-1 --param1 val1 --param2 val2 \
    --args-to-pass-on-2 bla --param3 val3 --blu

cannot be achieved with argparse.

argpass is a thin wrapper around argparse that allows you to do exactly that. When adding another paramater to the parser, simply specify nargs=NargsOption.COLLECT_UNTIL_NEXT_KNOWN and argpass will collect all strings until the next known argument:

$ cat script.py
import argpass

parser = argpass.ArgumentParser()
parser.add_argument("--file")
parser.add_argument("--regular-arg")
parser.add_argument(
    "--args-to-pass-on-1", nargs=argpass.NargsOption.COLLECT_UNTIL_NEXT_KNOWN
)
parser.add_argument(
    "--args-to-pass-on-2", nargs=argpass.NargsOption.COLLECT_UNTIL_NEXT_KNOWN
)
print(parser.parse_args())

$ python script.py \
    --file example.txt --regular-arg exampleArg \
    --args-to-pass-on-1 --param1 val1 --param2 val2 \
    --args-to-pass-on-2 bla --param3 val3 --blu
Namespace(file='example.txt', regular_arg='exampleArg', args_to_pass_on_1=['--param1', 'val1', '--param2', 'val2'], args_to_pass_on_2=['bla', '--param3', 'val3', '--blu'])

Metadata

Release files for argpass 0.0.2

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

Source distribution (sdist)

Source distribution for argpass 0.0.2
File Size Uploaded Provenance Actions
argpass-0.0.2.tar.gz 4.6 kB Without provenance Details for argpass-0.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 9.3 kB

Release files / argpass-0.0.2.tar.gz

Download URL argpass-0.0.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9814a06a38cddc513f55026409f8ad2cb66ddf78573b2e8dffb463561b069f39
BLAKE2b-256 checksum
How to use checksums
592fcc4e8ab1d00e729391be2b229029cb335ae28bce9afef4732a255121b54f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / argpass-0.0.2-py3-none-any.whl

Download URL argpass-0.0.2-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0804449cfd90005892f148cfb9a20f9065d848460f5228aa48a98e6c82597ee8
BLAKE2b-256 checksum
How to use checksums
7f9c43fee4ddf8896e0e5ddb43687f458aacf101275e397e6b8d087d6db00c04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Security for argpass 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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