Created
January 29, 2019 12:34
-
-
Save nafeu/e513e23b6b343ef896e4cccd4430b428 to your computer and use it in GitHub Desktop.
Python3 Terminal Script Template
This file contains 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 | |
# -*- coding: utf-8 -*- | |
"""Template for python3 terminal scripts. | |
This gist allows you to quickly create a functioning | |
python3 terminal script using argparse and subprocess. | |
""" | |
import argparse | |
import os | |
import sys | |
import subprocess | |
__author__ = "Author Name" | |
__copyright__ = "Copyright 2019, Author Org" | |
__credits__ = ["Author Name"] | |
__license__ = "MIT" | |
__version__ = "0.0.1" | |
__maintainer__ = "Author Name" | |
__email__ = "[email protected]" | |
__status__ = "Development" | |
parser = argparse.ArgumentParser(description='Script description') | |
# Positional Arguments | |
parser.add_argument('argument_name', | |
help="argument description", | |
nargs='?', | |
const=0) | |
# Optional Arguments | |
parser.add_argument("-f", "--foo", | |
help="specify foo", | |
action='store_true') | |
parser.add_argument("-b", "--bar", | |
help="specify bar", | |
metavar='BAR', | |
nargs=1) | |
args = parser.parse_args() | |
def main(): | |
if args.foo: | |
subprocess.run(["echo", "bar"]) | |
if args.argument_name: | |
print("Argument name: %s" % args.argument_name) | |
if args.bar: | |
print("Specified bar: %s" % args.bar) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment