Skip to content

Instantly share code, notes, and snippets.

@gcaracuel
Created June 27, 2022 21:31
Show Gist options
  • Save gcaracuel/2997f3fab0bf2393d95bc9647a3e524d to your computer and use it in GitHub Desktop.
Save gcaracuel/2997f3fab0bf2393d95bc9647a3e524d to your computer and use it in GitHub Desktop.
[Python] UNIX command skeleton but now using Piou -> https://github.com/Andarius/piou
#!/usr/bin/env python3
import sys
import traceback
from piou import Cli, Option
import time
import logging
cli = Cli(propagate_options=True,description=
"""
A CLI tool
""")
@cli.processor()
def processor(verbose: bool = Option(False, '--verbose', help='Increase verbosity')):
print(f'Processing {verbose=}')
### Global options ###
# cli.add_option('-q', '--quiet', help='Do not output any message')
### A command ###
# @cli.command(cmd='foo', help='Run foo command')
# def foo_main(
# foo1: int = Option(..., help='Foo arguments'),
# foo2: str = Option(..., '-f', '--foo2', help='Foo2 arguments'),
# foo3: str = Option(None, '-g', '--foo3', help='Foo3 arguments'),
# ):
# """
# A longer description on what the function is doing.
# You can run it with:
# ```bash
# poetry run python -m piou.test.simple foo 1 -f baz
# ```
# And you are good to go!
# """
# pass
### A sub command ###
# sub_cmd = cli.add_sub_parser(cmd='sub', help='A sub command')
# sub_cmd.add_option('--test', help='Test mode')
# @sub_cmd.command(cmd='bar', help='Run bar command')
# def sub_bar_main(**kwargs):
# pass
# @sub_cmd.command(cmd='foo', help='Run foo command')
# def sub_foo_main(
# test: bool,
# foo1: int = Option(..., help='Foo argument'),
# foo2: str = Option(..., '-f', '--foo2', help='Foo2 argument'),
# ):
# pass
if __name__ == '__main__':
try:
logging.debug('EXECUTION STARTS')
start_time = time.time()
cli.run()
logging.debug('TOTAL TIME OF EXECUTION IN MINUTES: ' + str(round((time.time() - start_time) / 60.0,2)))
sys.exit(0)
except KeyboardInterrupt as e: # Ctrl-C
raise e
except SystemExit as e: # sys.exit()
raise e
except Exception as e:
logging.error('ERROR, UNEXPECTED EXCEPTION')
logging.error(str(e))
traceback.print_exc()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment