New command line interface based on Python Rich library.
Project description
Argsense CLI
argsense is a command line interface made with Python.
Usage
Currently this section is under construction.
Please check the folder ./examples for your reference.
Comparison to other CLIs
I picked several official examples from Typer project to show differences by code to code.
Hello world
typer
import typer
def main(name: str):
typer.echo(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
argsense
from argsense import cli
@cli.cmd()
def main(name: str):
print(f'Hello {name}')
if __name__ == '__main__':
cli.run(main)
Multiple subcommands
typer
import typer
app = typer.Typer()
@app.command()
def hello(name: str):
typer.echo(f"Hello {name}")
@app.command()
def goodbye(name: str, formal: bool = False):
if formal:
typer.echo(f"Goodbye Ms. {name}. Have a good day.")
else:
typer.echo(f"Bye {name}!")
if __name__ == "__main__":
app()
argsense
from argsense import cli
@cli.cmd()
def hello(name: str):
print(f'Hello {name}')
@cli.cmd()
def goodbye(name: str, formal: bool = False):
if formal:
print(f'Goodbye Ms. {name}. Have a good day.')
else:
print(f'Bye {name}!')
if __name__ == '__main__':
cli.run()
Making an optional argument
typer
import typer
from typing import Optional
def main(name: Optional[str] = typer.Argument(None)):
if name is None:
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 argument
must be required, or it is considered as an `option`.
We will bring the support in next version (v0.4.0+).
"""
from argsense import cli
@cli.cmd()
def main(name: str = None):
if name is None:
print('Hello World!')
else:
print(f'Hello {name}')
if __name__ == '__main__':
cli.run(main)
You have to use --name
to pass the 'optional argument', otherwise it raises an error.
An argument with a default value
typer
import typer
def main(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 argument
must be required, or it is considered as an `option`.
We will bring the support in next version (v0.4.0+).
"""
from argsense import cli
@cli.cmd()
def main(name='Wade Wilson'):
print(f'Hello {name}')
if __name__ == '__main__':
cli.run(main)
Dynamic choices for argument
typer
import random
import typer
def get_name():
return random.choice(["Deadpool", "Rick", "Morty", "Hiro"])
def main(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.
"""
from argsense import cli
@cli.cmd()
def main(name: str):
"""
args:
name: The name should be one of the choices:
- [red]Deadpool[/]
- [green]Rick[/]
- [yellow]Morty[/]
- [cyan]Hiro[/]
"""
if name not in ('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 description
The 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.
typer
import typer
def main(
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, like
Wilson.
If --formal is used, say hi very formally.
"""
if formal:
typer.echo(f"Good day Ms. {name} {lastname}.")
else:
typer.echo(f"Hello {name} {lastname}")
if __name__ == "__main__":
typer.run(main)
argsense
from argsense import cli
@cli.cmd()
def main(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.
"""
if formal:
print(f'Good day Ms. {name} {lastname}.')
else:
print(f'Hello {name} {lastname}')
if __name__ == '__main__':
cli.run(main)
Confirmation Prompt
typer
import typer
def main(
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.
"""
from argsense import cli
@cli.cmd()
def main(name: str):
email = input('Email: ')
if input('Repeat for confirmation: ') != email:
print('Email repetition failed.')
exit(1)
print(f'Hello {name}, your email is {email}')
if __name__ == '__main__':
cli.run(main)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
argsense-0.4.1.tar.gz
(22.4 kB
view details)
Built Distribution
argsense-0.4.1-py3-none-any.whl
(25.1 kB
view details)
File details
Details for the file argsense-0.4.1.tar.gz
.
File metadata
- Download URL: argsense-0.4.1.tar.gz
- Upload date:
- Size: 22.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.1 Darwin/21.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09d20dd225724d96e301c48dd768bd935b959dc9e9ade879a6b718f0bcabad0f |
|
MD5 | da90d7b0d31b2e1c061b43474899fa70 |
|
BLAKE2b-256 | 61f28cd19068e852213b56a755f015e3680daf725b156f15325de910ae184ebe |
File details
Details for the file argsense-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: argsense-0.4.1-py3-none-any.whl
- Upload date:
- Size: 25.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.1 Darwin/21.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 047ef00be882fc9637e94f9ea0ca8da498c9abdf819666f267f7741f5c1fcefd |
|
MD5 | b3fd9ddd8fdb6e8a1493cb301f5fa77b |
|
BLAKE2b-256 | 67b95e14846a423cc513ea39a77bbadf66b9f0b8e9aad904f88bd56da5b2db62 |