#!/bin/env python """ Example of using the argument parser to create a simple hello program. - Author: Casey Walker - License: MIT Assuming this file is named `say-hello` and has execution permissions, it can be used like any normal command line tool: >>> say-hello Mary Poppins Hello, Mary Poppins! """ import sys from cliclops.parse import Arg_parser, Floating_spec, Positional_spec def main() -> int: help = Floating_spec("h?", "help", desc="Show the help message.") first_name = Positional_spec( "first-name", num_args=1, desc="The person's first name." ) last_name = Positional_spec( "last-name", is_unbounded=True, desc="The person's last name." ) ap = Arg_parser( [help, first_name, last_name], progname="say-hello", short_prefixes="-", long_prefixes="--", ) usage = f"""{ap.progname} [-h -? --help] <NAME> [<NAME>]""" (invokename, *parsed_args) = ap.parse() name: list[str] = [] for parsed_arg in parsed_args: spec = parsed_arg.spec if spec == help: print(usage) return 0 elif spec in [first_name, last_name]: name += parsed_arg.values else: if isinstance(spec, Floating_spec): print( f"Unknown floating argument: {parsed_arg.prefix}" f"{parsed_arg.floating_form}" ) else: print(f"Unknown positional argument: {parsed_arg.values}") print("Use '-h' to see usage.") return 1 if len(name) > 0: print(f"Hello, {' '.join(name)}!") else: print("No one to say hello to.") print("Use '-h' to see usage.") return 2 return 0 if __name__ == "__main__": sys.exit(main())