Skip to main content

ArgvParser

ArgvParser allows to format an argument vector in a structure easier to read and use for command line applications.

Installation

python -m pip install argvparser

Importation

import argvparser

Using

import argvparser
import sys

args = argvparser.argvparser.parse(sys.argv)

Documentation

ArgvParser.parse

Formate arguments vector to a dictionnary

:param argv: The arguments vector :type argv: list

:return: The formated arguments :rtype: dict

:raise Exception: Argument assigned to any option

:Example:

Parsing with several concatenated options

>>> argvparser.argvparser.parse(['app.py', 'ls', '-lar', '42', '--float', '3.14'])
{
    'app': 'app',
    'command': 'ls',
    'options': {
        '-l': None,
        '-a': None,
        '-r': 42,
        '--float': 3.14
    }
}

Parsing without command specified

>>> argvparser.argvparser.parse(['app.py', '--print', 'My message I want to print', '-i'])
{
    'app': 'app',
    'command': None,
    'options': {
        '--print': 'My message I want to print',
        '-i': None
    }
}

Parsing with duplicated options

>>> argvparser.argvparser.parse(['app.py', '-v', '/var/www', '-i', '-v', '/var/bin/bash'])
{
    'app': 'app',
    'command': None,
    'options': {
        '-v': [
            '/var/www',
            '/var/bin/bash'
        ],
        '-i': None
    }
}

ArgvParser.parse_multi_options

Retrieves multiple argument (like -li) and reconstruct it to a correct format

:param argv: The arguments vector :type argv: list

:return: Correctly formated arguments vector :rtype: list

:Example:

>>> argvparser.argvparser.parse_multi_options(['app.py', '-liar', '--test'])
['app.py', '-l', '-i', '-a', '-r', '--test']

ArgvParser.is_option

Check if the argument in parameter is an option or not

:param arg: The argument to control :type arg: str

:return: True if arg is an option, else false :rtype: bool

:Example:

>>> argvparser.argvparser.is_option('-t')
True

>>> argvparser.argvparser.is_option('--test')
True

>>> argvparser.argvparser.is_option('test')
False

ArgvParser.parse_type

Convert the passed argument into the appropriate type

:param arg: The argument to convert :type arg: str, None

:return: The converted argument :rtype: int, float, str, None

:note: The default type returned is a str

:Example:

>>> argvparser.argvparser.parse_type('test')
'test'

>>> argvparser.argvparser.parse_type('42')
42

>>> argvparser.argvparser.parse_type('3.14')
3.14

Metadata

Release files for argvparser 1.0.0

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

Source distribution (sdist)

Source distribution for argvparser 1.0.0
File Size Uploaded
argvparser-1.0.0.tar.gz 2.9 kB Details

Built distribution (wheel)

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

Total release size: 6.5 kB

Release files / argvparser-1.0.0.tar.gz

Download URL argvparser-1.0.0.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3b818205c61765e93b98bd0d4e1458e98c8346eda67385726462c41d1ef2888e
BLAKE2b-256 checksum
How to use checksums
1ac33216c9b1fb4b3cea521e2407256ca7fe8ef49b7e420eb5d27dcf45cd9c7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / argvparser-1.0.0-py3-none-any.whl

Download URL argvparser-1.0.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd261e7158ea795b1df2fdff7ea86ba117de27a89f87b12abba8f985d1da2d97
BLAKE2b-256 checksum
How to use checksums
8b31e03f21f638119be2d6f6af78b3b6e18b8c435890b160fa4ed139d1425c44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.2

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