argparse#

class argparse(parse=True, **kwargs)[source]#

Bases: objdict

Ultra-simple argument parser

Accepts positional or keyword arguments, and converts them to the correct type. While Python’s built in argparse has more features (such as help for each argument), this allows single-line parsing of arguments.

Parameters:
  • parse (bool) – whether to parse the arguments immediately (default True)

  • **kwargs (dict) – keyword arguments to add to the parser

Returns:

a dictionary-like object with the arguments

Return type:

args (objdict)

Examples:

# Option 1: Supply arguments directly
args = sc.argparse(iterations=10, output_file='results.csv')

# Option 2: Add arguments one by one
args = sc.argparse()
args.add(iterations=10)
args.add(output_file='results.csv')
args.parse()

# Command-line usage
python argparse_example.py 100 'data.csv'
python argparse_example.py 100 output_file='data.csv'
python argparse_example.py iterations=100 --output_file='data.csv'

# Result
args.iterations == 10
args.output_file == 'data.csv'

New in version 3.2.6.

Methods

add(**kwargs)[source]#

Add an argument

parse()[source]#

Parse the arguments into the dictionary