Skip to content

Parsing Methods

Sean Collings edited this page Jul 17, 2021 · 3 revisions

Arc provides several different Parsing Methods that change the way that input is interpreted.

Standard

Default parsing method. Assumes input in the form of --name value --name2 value2 like a standard command line tool.

Example

from arc import CLI
from arc import ParsingMethod as pm

cli = CLI()

@cli.subcommand("greeting", pm.STANDARD) # Since it is the default, specifying here is optional
def test(name="Jotaro Kujo"):
    print(f"Greeting, {name}!")

cli()
$ python3 example.py greeting --name Sean Collings
Greeting, Sean Collings!

Can also accept **kwargs

Keyword

Assumes that input is in the form argname=value. Will throw an error if given any positional arguments. Will except flags normally

Examples

from arc import CLI
from arc import ParsingMethod as pm
cli = CLI()

@cli.subcommand("greeting", pm.KEYWORD)
def test(name="Jotaro Kujo"):
    print(f"Greeting, {name}!")

cli()
$ python3 example.py greeting name="Sean"
Greeting, Sean!

Can also accept **kwargs

Positional

Assumes input in the form of value1 value2 value3. Acts similarly to positional function arguments in Python and will be passed in the order recieved. Will throw an error if any keyword options are given.

Example

from arc import CLI
from arc import ParsingMethod as pm

cli = CLI()

@cli.subcommand("greeting", pm.POSITIONAL)
def test(name="Jotaro Kujo"):
    print(f"Greeting, {name}!")

cli()
$ python3 example.py greeting Sean
Greeting, Sean!

Positional Scripts also accept *args as the final argument. If it is any other argument, Arc will throw an error. Does not accept **kwargs

from arc import CLI
from arc import ParsingMethod as pm

cli = CLI()

@cli.subcommand("greeting", pm.POSITIONAL)
def test(name="Jotaro Kujo", *args):
    print(f"Greeting, {name}!")
    if len(args) > 0:
        for val in args:
            print(val)

cli()
$ python3 example.py greeting "Sean" 19 "Programmer"
Greeting, Sean!
19
Programmer

Raw

Raw commands won't do anything to the input and pass it to your function exactly as recieved. Essentially, it acts as just a wrapper around sys.argv. Primarily useful for when you want to take full control over how the input is interpreted for a specific command or namespace.

Examples

from arc import CLI
from arc import ParsingMethod as pm

cli = CLI()

@cli.subcommand("raw", pm.RAW)
def test(*args):
    print("Here the input!")
    print(*args)

cli()
$ python3 example.py raw option=val orphan --flag option="val with spaces"
Here's the input!
option=val orphan --flag option=val with spaces

Parsing Method is inherited

Parsing Method can be specified on the CLI, namespace, or Command level.

from arc import CLI, namespace, ParsingMethod as pm

cli = CLI(parsing_method=pm.KEYWORD) # At CLI level

ns = namespace("namespace", parsing_method=pm.POSITIONAL) # at a namespace level

@ns.subcommand("greeting", pm.RAW)  # At Command level
def greeting(*args):
    print(args)

If the command type is not defined on the command, it inherits the type of it's parent.

Clone this wiki locally