$ git clone https://github.com/JnyJny/cli-examples
$ cd cli-examples
$ poetry shell
$ poetry install
$ python3 -m venv .venv
$ source .venv/bin/activate
$ python3 -m pip install -r requirements.txt
The contents of cli/00-dump-argv/__main__.py
:
""" introduction to sys.argv
Prints the contents of sys.argv and exits with status 11
"""
import sys
if __name__ == "__main__":
for n, item in enumerate(sys.argv):
print(f"argv[{n}] == {item}")
exit(11)
$ python3 -m cli.00-dump-argv foo -b baz -a --ack
argv[0] == /Users/ejo/local/cli-examples/cli/00-dump-argv/__main__.py
argv[1] == foo
argv[2] == -b
argv[3] == baz
argv[4] == -a
argv[5] == --ack
The contents of cli/10-argv/__main__.py
:
""" copy file implemented with sys.argv
"""
import sys
if __name__ == "__main__":
# default values
inputfile = sys.stdin
outputfile = sys.stdout
flag = False
# sys.argv[0] is the name of the program
for ndx, item in enumerate(sys.argv[1:]):
if item in ["-i", "--input-file"]:
inputfile = open(sys.argv[ndx + 1], "r")
continue
if item in ["-o", "--output-file"]:
outputfile = open(sys.argv[ndx + 1], "w")
continue
if item in ["-f", "--flag"]:
flag = True
continue
if item in ["-h", "--help", "-?"]:
print(
sys.argv[0],
"[--input-file name] [--output-file name] [--flag]",
file=sys.stderr,
)
exit(1)
raise ValueError(f"unexpected option {item}")
print("flag", flag)
outputfile.write(inputfile.read())
The contents of cli/20-argpase/__main__.py
:
""" copy file implemented with argparse
"""
from argparse import ArgumentParser, FileType
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--input-file", "-i", type=FileType("r"), default="-")
parser.add_argument("--output-file", "-o", type=FileType("w"), default="-")
parser.add_argument("--flag", "-f", action="store_true")
args = parser.parse_args()
print("flag", args.flag)
args.output_file.write(args.input_file.read())
The contents of cli/30-typer/__main__.py
:
""" copy file implemented with typer
"""
import typer
cli = typer.Typer()
context_settings = dict(help_option_names=["--help", "-h"])
@cli.command(context_settings=context_settings)
def copy_files(
input_file: typer.FileText = typer.Option("-"),
output_file: typer.FileTextWrite = typer.Option("-"),
flag: bool = False,
) -> None:
"""Copy input file to the named output file."""
print("flag", flag)
output_file.write(input_file.read())
if __name__ == "__main__":
cli()
""" copy file implemented with click
"""
import click
@click.command()
@click.option("input-file", type=click.File("r"))
@click.option("output-file", type=click.File("w"))
def copy_files(input_file, output_file, flag):
"""Copy input file to the named output file."""
print("flag", flag)
output_file.write(input_file.read())
if __name__ == "__main__":
copy_files()