Last active
September 27, 2024 12:04
-
-
Save ShangjinTang/2ca84562530d69215e9416f035e3f054 to your computer and use it in GitHub Desktop.
Templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This is a docstring. | |
""" | |
import argparse | |
import logging | |
import os | |
import sys | |
import argcomplete | |
__author__ = "Shangjin Tang" | |
__email__ = "[email protected]" | |
__copyright__ = "Copyright 2024 Shangjin Tang" | |
__license__ = "GPL" | |
__version__ = "1.0.0" | |
LOGGING_FORMAT = "%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d - %(message)s" | |
LOGGING_DATEFMT = "%Y-%m-%d %H:%M:%S" | |
logging.basicConfig( | |
level=logging.DEBUG, | |
format=LOGGING_FORMAT, | |
datefmt=LOGGING_DATEFMT, | |
) | |
class UltimateHelpFormatter( | |
argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter | |
): | |
pass | |
def main(args): | |
print(args) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
formatter_class=UltimateHelpFormatter, description="" | |
) | |
parser.add_argument("-i", "--input", type=str, help="input") | |
parser.add_argument("-o", "--output", type=str, help="output") | |
parser.add_argument( | |
"-c", | |
"--choice", | |
default="choice_x", | |
choices=["choice_x", "choice_y", "choice_z"], | |
) | |
# You can use mutex groups for conflicted commands | |
# group = parser.add_mutually_exclusive_group() | |
# group.add_argument('--foo', action='store_true') | |
# group.add_argument('--bar', action='store_false') | |
# You can use subparsers for complex commands | |
# subparsers = parser.add_subparsers(title="subcommands") | |
try: | |
args = parser.parse_args() | |
main(args) | |
except KeyboardInterrupt: | |
print("Interrupted") | |
try: | |
sys.exit(0) | |
except SystemExit: | |
os._exit(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This is a docstring. | |
""" | |
import logging | |
import os | |
import sys | |
import typer | |
__author__ = "Shangjin Tang" | |
__email__ = "[email protected]" | |
__copyright__ = "Copyright 2024 Shangjin Tang" | |
__license__ = "GPL" | |
__version__ = "1.0.0" | |
LOGGING_FORMAT = "%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d - %(message)s" | |
LOGGING_DATEFMT = "%Y-%m-%d %H:%M:%S" | |
logging.basicConfig( | |
level=logging.DEBUG, | |
format=LOGGING_FORMAT, | |
datefmt=LOGGING_DATEFMT, | |
) | |
app = typer.Typer() | |
@app.command() | |
def main( | |
input_file: str = "", | |
output_file: str = "", | |
): | |
logging.debug(f"Input file: {input_file}") | |
logging.debug(f"Output file: {output_file}") | |
if __name__ == "__main__": | |
try: | |
typer.run(main) | |
except KeyboardInterrupt: | |
typer.echo("Interrupted") | |
try: | |
sys.exit(0) | |
except SystemExit: | |
os._exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment