0

Some of you might know this script, it's called hash-identifier. When it is run the user is prompted to enter a hash. I want to pass the hash as a command line argument so that the script can be executed like that:

hash-identifier d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f

I found out that I need to import sys and getopt but I have never used python before so any suggestions would be helpful.

tshepang
12.5k25 gold badges98 silver badges140 bronze badges
asked Feb 12, 2014 at 21:35

3 Answers 3

2

the preferred method is to use argparse:

#!/usr/bin/env python
import argparse
if __name__ == "__main__":
 parser = argparse.ArgumentParser(description="Does something with a hash");
 parser.add_argument("hash", metavar="HASH", help="the hash to do things with?");
 args = parser.parse_args();
 hash = args.hash;
 # Use the hash...
 print(hash);

But using argparse might be a bit overkill for your needs, it may be simpler for you to do this:

#!/usr/bin/env python
import sys
if __name__ == "__main__":
 if len(sys.argv) != 2: # first is program name, second is argument
 print("USAGE: %s HASH"%(sys.argv[0],));
 sys.exit();
 hash = sys.argv[1];
 # Use the hash...
 print(hash);
answered Feb 12, 2014 at 21:46
Sign up to request clarification or add additional context in comments.

Comments

0

Ok, after I imported sys, the only thing I need to do is pass the sys.argv to the variable being printed. Example:

variable = sys.argv
print variable
answered Feb 12, 2014 at 21:42

Comments

0

You can either use sys.argv[0] to get the first command line argument to the script. Or the argparse module if you want more options.

answered Feb 12, 2014 at 21:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.