Issue
Here are the instruction for how I'm supposed to build my program:
Use argparse to implement the CLI portion of the program so it works as shown here. It is one of the few programs in this course that actually prints output.
Output from the program should look exactly like this when you use the -h help flag. Hint: You get the -h help flag for free with argparse. You do not have to implement the -h flag yourself.
$ python HW3_cli.py -h usage: HW3_cli.py [-h] [-p] [-c] [-l] texts [texts ...]
positional arguments: texts Input strings to operate on
optional arguments: -h, --help show this help message and exit -p, --print Just print the input strings -c, --combine Print input strings combined in a continuous string
-l, --len Print the lengths of the input strings
Does print, combine, then len. If no flags given, does nothing.
See that description line at the end telling what it does? It is created with the epilog.
If no arguments are given at all, it should give an error that the texts arguments are required. Hint: You get this error for free if you program the texts argument correctly.
$ python HW3_cli.py usage: HW3_cli.py [-h] [-p] [-c] [-l] texts [texts ...]
HW3_cli.py: error: the following arguments are required: texts
The behavior of the three flags are explained below. If at least one input string is given, but no flags are given, the program should do nothing. Since there are no flags used in the lecture examples, you will need to check the argparse documentation (look at the tutorial here) to find how to implement flag arguments. They are called "Short options" in the documentation. Look for the example of implementing the -v "verbose" option.
Arguments
The flag arguments control what the program does. The flags can be given in any order or combination on the command line, however, these are the rules for implementation: They should be implemented in the order shown below - in other words, the print flag (if given) is executed first, then the combine flag (if given), then the len flag (if given). Please make all the help strings exactly as shown above in the output from using help, as shown above. Feel free to copy/paste from this document.
The -p or --print flag will print out the input strings with spaces in between each string. We know how to make one string from a list of strings with one of the str methods.
The -c or --combine flag will print all the input strings concatenated together. Again, we know how to do that with a string method.
The -l or --len flag prints out the lengths of each of the input strings. No, you don't need to use len_safe, since they are always strings. This is a bit more challenging, but you can figure it out! Options for implementation of -l or --len include generator expressions, special arguments for the print function, and others.
Here is how my code should look like when it runs:
$ python HW3_cli.py -c These Strings Get Concatenated TheseStringsGetConcatenated $ python HW3_cli.py -c -p These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated $ python HW3_cli.py -l -c -p These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated 5 7 3 7 3 12 $ python HW3_cli.py -c -l --print These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated 5 7 3 7 3 12 $ python HW3_cli.py --len --combine --print These Strings Get Printed And Concatenated These Strings Get Printed And Concatenated TheseStringsGetPrintedAndConcatenated 5 7 3 7 3 12 $ python HW3_cli.py --len --combine a b c d e f g abcdefg 1 1 1 1 1 1 1 $ python HW3_cli.py testing $ python HW3_cli.py -l testing 7$ python HW3_cli.py -p testing testing $ python HW3_cli.py -c testing testing
Lastly, here is my code, and then I will share the error I get:
import argparse
if __name__ == "__main__":
# Set up argparse parser and arguments here
parser = argparse.ArgumentParser(epilog="Does print, combine, then len. If no flags given, does nothing.")
# Add the arguments here
parser.add_argument('texts', help='Input strings to operate on')
parser.add_argument('-p', '--print', action='store_true', help='Just print the input strings')
parser.add_argument('-c', '--combine', action='store_true', help='Print input strings combined in a continuous string')
parser.add_argument('-l', '--len', action='store_true', help='Print the lengths of the input strings')
args = parser.parse_args()
if args.print:
words = split(str(args.texts))
print(" ".join(words))
if args.combine:
words2 = split(str(args.texts))
print("".join(words2))
if args.len:
for text in split(str(args.texts)):
print(len(text))
Lastly, the error I get is: $ python HW3_cli.py -c These Strings Get Concatenated usage: HW3_cli.py [-h] [-p] [-c] [-l] texts HW3_cli.py: error: unrecognized arguments: Strings Get Concatenated
Solution
As mentioned in comments, if you want to pass in a multi-word input for texts
, then they will need to be wrapped in quotes like ./script "a b c" -p
.
However, here's an approach where you don't need to wrap it in quotes. This specifies nargs='*'
for the positional argument texts
, which is a greedy quantifier that matches as many arguments as you pass in to the script. The parsed result will then be passed in as a list of words, so we don't need to split the input string either.
import argparse
if __name__ == "__main__":
# Set up argparse parser and arguments here
parser = argparse.ArgumentParser(epilog="Does print, combine, then len. If no flags given, does nothing.")
# Add the arguments here
parser.add_argument('texts', help='Input strings to operate on',
# Add `nargs=*` so we capture as many positional arguments as possible.
# The parsed result will be passed in as a list.
nargs='*')
parser.add_argument('-p', '--print', action='store_true', help='Just print the input strings')
parser.add_argument('-c', '--combine', action='store_true',
help='Print input strings combined in a continuous string')
parser.add_argument('-l', '--len', action='store_true', help='Print the lengths of the input strings')
args = parser.parse_args()
if args.print:
print(" ".join(args.texts))
if args.combine:
print("".join(args.texts))
if args.len:
# prints output on a single line, separated by spaces
print(*map(len, args.texts))
# alternatively, if you want output on each line:
# for text in args.texts:
# print(len(text))
Example usage:
$ python script.py -lcp These Strings Get Printed And Concatenated
These Strings Get Printed And Concatenated
TheseStringsGetPrintedAndConcatenated
5 7 3 7 3 12
Answered By - rv.kvetch
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.