Command-line programs · 9 / 15 Exercise Medium

Counter like wc

Objective

Create a program that imitates the Unix wc command: it counts lines, words and characters of the text it receives through standard input, and is controlled with flags (options without a value). The arguments of each test are passed to the program as if you typed them in the terminal (they appear in the test cases).

Instructions

  • Define with argparse the flags -l/--lines, -w/--words and -c/--chars (with action="store_true").
  • Read all the text with sys.stdin.read(). Lines are obtained with splitlines(); words, with split(); characters are those of the lines (without the line breaks).
  • Show Lines: k, Words: k and/or Characters: k depending on the flags given. If none is given, show the three, in that order.
Input
hola mundo
otra linea mas
Output
Lines: 2
Words: 5
Characters: 24

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise