Open of a file should always be paired with a close when you are done using it, as is also true for other I/O objects such as network connections. f instead of -f True) to set it.Īfter that, you can access your inputs as a strings (because type=str) by args.input (because the first argument to add_argument was "input"). action="store_true" means that the flag has to be specified without a value (i.e. dest=file means that the created object args wil have an attribute args.file that stores the resulting value. "-f", "-file" are the flags you can use to specify this argument. Instead of using -single or -list, I added a single flag -file (or -f), as the former choice is not only ambiguous (what happens if you specify neither or both?) as well as potentially misleading in their name.Ī help option with "-h" is introduced automatically. import argparseĪrg_parser.add_argument("-f", "-file", dest="file", default=False, action="store_true")Īrg_parser.add_argument("input", type=str) All of that can be done in three lines with argparse. Most of your code is used to parse and use the command line arguments. You should use the newer and easier argparse instead. The biggest opportunity for improvement is your use of getopt. # If only one argument is supplied, print MAN page. # If option -l is present read file specified then loop through each line while splitting each into username and domain # If option -s is present, split input email into username and domain then print the output # If option -h is present, display MAN page "-s, -single Specify a single email to be sliced \n" "-l, -list Specify a list of emails to be sliced \n" Opts, args = getopt.getopt(argv, "l:s:h", ) The goal of the program is to separate an email address into its username and domain name. This is my first ever Python program and I wanted to get everyone's opinions on it.
0 Comments
Leave a Reply. |