PK /lHAÃ9¤Ž Ž argon-latest/searchindex.jsSearch.setIndex({objects:{"":{argon:[4,0,1,""]},"argon.App":{run:[4,1,1,""],sub:[4,1,1,""],run_known:[4,1,1,""],parse:[4,1,1,""],command:[4,1,1,""],parse_known:[4,1,1,""],arg:[4,1,1,""]},argon:{App:[4,2,1,""],Command:[4,2,1,""],Sub:[4,2,1,""]},"argon.Sub":{command:[4,1,1,""],sub:[4,1,1,""],arg:[4,1,1,""]},"argon.Command":{handler:[4,1,1,""],arg:[4,1,1,""]}},terms:{code:[0,3],suberror:2,help:[1,3],show:3,test_run_known:2,test_arg:2,using_argon_with_valid:1,group:[4,3],set_default:1,add:[4,3],els:3,main:[2,3],modul:0,applic:4,sourc:[0,3,4],"return":[4,2,3],string:3,fals:[2,3],number:3,print:[1,3],assertrais:2,do_sum:3,math_sum:3,choic:1,same_command_command_nam:2,bar:1,name:4,level:1,xyz:1,revers:3,list:2,run_known:[4,2],assertequ:2,math:3,output:[4,3],page:0,upper:3,set:4,baz:1,math_hex:3,possibl:3,parsed_arg:4,integ:3,do_echo:3,pass:[4,2],testcas:2,add_subpars:1,test_name_collis:2,index:0,abb:2,abc:2,sub:[0,2,3,4],helper:0,sum:3,content:0,exit:3,test_sub_simple_ok:2,prog:1,"import":[1,2,3],foo:[1,2],method:4,same_sub_command_nam:2,refer:[0,4],two_handl:2,run:[4,1,2,3],join:3,usag:[0,3],argv:[4,3],"__main__":[1,2,3],ooof:3,"__name__":[1,2,3],comb:2,valu:3,search:0,test_command_no_handl:2,parse_known:[4,2],constructor:4,action:[1,2,3],app:[4,1,2,3],lambda:2,using_argon_without_valid:1,kwd:4,aacb:2,aaca:2,argumentpars:[4,1],echo:3,test_command_ok:2,api:[0,4],"0x12d687":3,wrapper:3,test_pars:2,instal:0,given:3,from:4,creat:[4,1],unknown:[4,2],top:1,hex:3,two:4,messag:3,do_hex:3,handler:[4,1,2,3],return_valu:2,type:[1,3],parse_arg:[4,1],"function":4,parser_a:1,parser_b:1,parser:[4,1],option:3,python:3,tupl:4,convert:3,argument:[4,1,3],argon:[0,1,2,3,4],pars:[4,1,2],accept:[4,3],func:[4,1,2,3],return_unknown:2,known:4,subpars:[4,1],line:4,"true":2,must:4,aaa:2,input:3,aac:2,aab:2,"default":[2,3],dest:[2,3],three:4,str:3,posit:3,motiv:[0,1],def:[1,2,3],argpars:[4,1],comput:3,abca:2,abcb:2,"int":[1,3],descript:3,arg:[4,1,2,3],add_argu:[4,1],indic:0,repres:4,fooo:3,add_pars:1,tabl:0,pip:0,unittest:2,baz_help:1,decor:3,store_tru:[1,2,3],self:[4,2],same:4,handl:1,split:2,test:[0,2],"new":4,narg:[2,3],singl:4,uppercas:3,using_argpars:1,thi:[4,3],aba:2,"class":[4,2],same_sub_sub_nam:2,commanderror:2,test_nest:2,typic:4,handle_b:1,handle_a:1,test_parse_known:2,test_command_two_handl:2,asserti:2,exampl:[0,3],command:[0,1,2,3,4],allow:4,callabl:4,testargon:2,no_handl:2},objtypes:{"0":"py:module","1":"py:method","2":"py:class"},titles:["Argon","Motivation","Tests","Example","API Reference"],objnames:{"0":["py","module","Python module"],"1":["py","method","Python method"],"2":["py","class","Python class"]},filenames:["index","motivation","ref/tests","example","ref/api"]})PK /lHAi¥@T6' 6' argon-latest/index.html
import sys
import argparse
import argon
def using_argparse(args):
# create the top-level parser
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("--foo", action="store_true", help="foo help")
subparsers = parser.add_subparsers()
# create the parser for the "a" command
parser_a = subparsers.add_parser("a", help="a help")
parser_a.add_argument("bar", type=int, help="bar help")
parser_a.set_defaults(func=handle_a)
# create the parser for the "b" command
parser_b = subparsers.add_parser("b", help="b help")
parser_b.add_argument("--baz", choices="XYZ", help="baz help")
parser_b.set_defaults(func=handle_b)
# parse arguments and run handler
parsed = parser.parse_args(args)
parsed.func(parsed)
def using_argon_with_validation(args):
# create the top-level parser
app = argon.App(prog="PROG")
app.arg("--foo", action="store_true", help="foo help")
# create the parser for the "a" command
with app.command("a", help="a help") as a:
a.arg("bar", type=int, help="bar help").handler(handle_a)
# create the parser for the "b" command
with app.command("b", help="b help") as b:
b.arg("--baz", choices="XYZ", help="baz_help").handler(handle_b)
# parse arguments and run handler
app.run(args)
def using_argon_without_validation(args):
# create the top-level parser
app = argon.App(prog="PROG")
app.arg("--foo", action="store_true", help="foo help")
# create the parser for the "a" command
app.command("a", help="a help") \
.arg("bar", type=int, help="bar help") \
.handler(handle_a)
# create the parser for the "b" command
app.command("b", help="b help") \
.arg("--baz", choices="XYZ", help="baz_help") \
.handler(handle_b)
# parse arguments and run handler
app.run(args)
def handle_a(args):
print "handling a", args.foo, args.bar
def handle_b(args):
print "handling b", args.foo, args.baz
if __name__ == "__main__":
for using in [using_argparse, using_argon_with_validation,
using_argon_without_validation]:
print "using", using
for args in [["a", "12"], ["--foo", "b", "--baz", "Z"]]:
using(args)
print
$ python example.py -h
usage: example.py [-h] [-r] {math,echo} ...
Argon example.
positional arguments:
{math,echo}
optional arguments:
-h, --help show this help message and exit
-r, --reverse reverse output
$ python example.py math -h
usage: example.py math [-h] {sum,hex} ...
positional arguments:
{sum,hex}
hex convert number to hex
optional arguments:
-h, --help show this help message and exit
$ python example.py math sum 1 2 3
6
$ python example.py math hex 1234567
0x12d687
$ python example.py echo -h
usage: example.py echo [-h] [-u] string
positional arguments:
string
optional arguments:
-h, --help show this help message and exit
-u convert to uppercase
$ python example.py echo fooo
fooo
$ python example.py -r echo -u fooo
OOOF
import sys
import argon
def main():
app = argon.App(description="Argon example.")
# If given, the output is reversed.
app.arg("-r", "--reverse", default=False, action="store_true",
help="reverse output")
# Add math sub-commands. "sum" computes the sum args and "hex" converts
# an integer to hex.
with app.sub("math") as math:
with math.command("sum") as math_sum:
math_sum.arg("numbers", help="numbers to compute sum of",
nargs="+", type=int)
math_sum.handler(do_sum)
with math.command("hex", help="convert number to hex") as math_hex:
math_hex.arg("number", type=int).handler(do_hex)
# Add an echo command. It accepts an argument to uppercase its input.
with app.command("echo") as echo:
echo.arg("-u", dest="uppercase", default=False, action="store_true",
help="convert to uppercase") \
.arg("string") \
.handler(do_echo)
app.run(sys.argv[1:])
def show(func):
"""Decorator to print the return value of func, possibly reversed. """
def wrapper(args):
output = str(func(args))
if args.reverse:
print "".join(reversed(output))
else:
print output
return wrapper
@show
def do_sum(args):
return sum(args.numbers)
@show
def do_hex(args):
return hex(args.number)
@show
def do_echo(args):
if args.uppercase:
return args.string.upper()
return args.string
if __name__ == "__main__":
main()
Please activate JavaScript to enable the search functionality.
From here you can search these documents. Enter your search words into the box below and click "search". Note that the search function will automatically search for all of the words. Pages containing fewer words won't appear in the result list.