Package: argsense
Argsense CLIargsenseis a command line interface made with Python.UsageCurrently this section is under construction.Please check the folder ./examples for your reference.Comparison to other CLIsI picked several official examples fromTyperproject to show differences by code to code.Hello worldtyperimporttyperdefmain(name:str):typer.echo(f"Hello{name}")if__name__=="__main__":typer.run(main)argsensefromargsenseimportcli@cli.cmd()defmain(name:str):print(f'Hello{name}')if__name__=='__main__':cli.run(main)Multiple subcommandstyperimporttyperapp=typer.Typer()@app.command()defhello(name:str):typer.echo(f"Hello{name}")@app.command()defgoodbye(name:str,formal:bool=False):ifformal:typer.echo(f"Goodbye Ms.{name}. Have a good day.")else:typer.echo(f"Bye{name}!")if__name__=="__main__":app()argsensefromargsenseimportcli@cli.cmd()defhello(name:str):print(f'Hello{name}')@cli.cmd()defgoodbye(name:str,formal:bool=False):ifformal:print(f'Goodbye Ms.{name}. Have a good day.')else:print(f'Bye{name}!')if__name__=='__main__':cli.run()Making an optional argumenttyperimporttyperfromtypingimportOptionaldefmain(name:Optional[str]=typer.Argument(None)):ifnameisNone:typer.echo("Hello World!")else:typer.echo(f"Hello{name}")if__name__=="__main__":typer.run(main)argsense"""Argsense (v0.3.0) doesn't have a conception of 'optional' argument, an argumentmust be required, or it is considered as an `option`.We will bring the support in next version (v0.4.0+)."""fromargsenseimportcli@cli.cmd()defmain(name:str=None):ifnameisNone:print('Hello World!')else:print(f'Hello{name}')if__name__=='__main__':cli.run(main)You have to use--nameto pass the 'optional argument', otherwise it raises an error.An argument with a default valuetyperimporttyperdefmain(name:str=typer.Argument("Wade Wilson")):typer.echo(f"Hello{name}")if__name__=="__main__":typer.run(main)argsense"""Argsense (v0.3.0) doesn't have a conception of 'optional' argument, an argumentmust be required, or it is considered as an `option`.We will bring the support in next version (v0.4.0+)."""fromargsenseimportcli@cli.cmd()defmain(name='Wade Wilson'):print(f'Hello{name}')if__name__=='__main__':cli.run(main)Dynamic choices for argumenttyperimportrandomimporttyperdefget_name():returnrandom.choice(["Deadpool","Rick","Morty","Hiro"])defmain(name:str=typer.Argument(get_name)):typer.echo(f"Hello{name}")if__name__=="__main__":typer.run(main)argsense"""Argsense doesn't have built in support for choices.Here is a workaround but not so good."""fromargsenseimportcli@cli.cmd()defmain(name:str):"""args:name: The name should be one of the choices:- [red]Deadpool[/]- [green]Rick[/]- [yellow]Morty[/]- [cyan]Hiro[/]"""ifnamenotin('Deadpool','Rick','Morty','Hiro'):print(f'The given name ({name}) is not on the list!')exit(1)print(f'Hello{name}')if__name__=='__main__':cli.run(main)Help information / Command descriptionThe example shows:Command description.Long description that exceeds 80 characters one line (let's see how do we do line breaks).Multiple lines of description.typerimporttyperdefmain(name:str=typer.Argument(...,help="The name of the user to greet"),lastname:str=typer.Option("","--lastname","-l",help="Last name of person to greet."),formal:bool=typer.Option(False,help="Say hi formally."),):"""Say hi to NAME very gently, like Dirk. Optionally with a --lastname/-l, likeWilson.If --formal is used, say hi very formally."""ifformal:typer.echo(f"Good day Ms.{name}{lastname}.")else:typer.echo(f"Hello{name}{lastname}")if__name__=="__main__":typer.run(main)argsensefromargsenseimportcli@cli.cmd()defmain(name:str,lastname='',formal=False):"""Say hi to [blue]NAME[/] very gently, like Dirk. Optionally with a -[dim]--lastname/-l[/], like Wilson.If [dim]--formal[/] is used, say hi very formally.args:name: The name of the user to greet.kwargs:lastname (-l): Last name of person to greet.formal: Say hi formally."""ifformal:print(f'Good day Ms.{name}{lastname}.')else:print(f'Hello{name}{lastname}')if__name__=='__main__':cli.run(main)Confirmation Prompttyperimporttyperdefmain(name:str,email:str=typer.Option(...,prompt=True,confirmation_prompt=True)):typer.echo(f"Hello{name}, your email is{email}")if__name__=="__main__":typer.run(main)argsense"""Argsense doesn't have built in support for confirmation prompt.Here is a workaround but not so good."""fromargsenseimportcli@cli.cmd()defmain(name:str):email=input('Email: ')ifinput('Repeat for confirmation: ')!=email:print('Email repetition failed.')exit(1)print(f'Hello{name}, your email is{email}')if__name__=='__main__':cli.run(main)