Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/env/python
# argparse_sysargv_example2.py <File.txt> <justdoit>
# Original script for https://blog.slowb.ro
import sys
import argparse
if __name__ == "__main__":
# Our default argparse function
argparser = argparse.ArgumentParser(description="Slowbro Hidden Power Sample 2")
argparser.add_argument("--file", action="store", help="File")
argparser.add_argument("--action", action="store", help="Action to take")
# You must create the variables we test against
file = ""
action = ""
if sys.argv[1][0:2] == "--":
arguments = argparser.parse_args()
if arguments.file:
file = arguments.file
if arguments.action:
action = arguments.action
else:
try:
if file == "":
file = sys.argv[1]
if action == "":
action = sys.argv[2]
except Exception as e:
print("Exception: %s" % e)
argparser.print_help()
sys.exit(1)
try:
contents = open(file, 'r').read()
except Exception as e:
print("Exception: %s" % e)
sys.exit(1)
if action == "justdoit":
print("%s" % contents)
else:
print("I have a hidden power, it's not scripting")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.