Argument parser for a word count and/or line count program How to make code dry in this example? My program is working, but I want to improve it by shorting my code.
.

parser = argparse.ArgumentParser()
parser.add_argument('--words', '-w', action='store_true')
parser.add_argument('--lines', '-l', action='store_true')
parser.add_argument('filename')
args = parser.parse_args()

if args.words:
    print "Words in a file: {}" .format(print_words(args.filename))

elif args.lines:
    print "Lines in a file: {}" .format(print_lines(args.filename))

else:
    print "Words in a file: {}" .format(print_words(args.filename))
    print "Lines in a file: {}" .format(print_lines(args.filename))


I guess by adding a new function and putting the function call in the else statement.

I tried with the print_all function, I added the function call in the else statement.

def print_all(filename, args):
    for a in read_file(filename):
        if a in args:
            print "All word counts :", a


When I run the program using the print_all function I get:


Namaspace(words=False, filename='someFile.txt', lines=False)
Do you notice something fishy on this line?


print "Words in a file: {}" .format(print_words(args.filename))



If I highlight just the fishy part:


print print_words(...)



The fishy part is printing something returned by print_words.
The name of print_words suggests that it prints something.
And printing functions typically don't return a value.
So this is all very confusing.

My guess is that print_words doesn't actually print words,
but returns a word count. So a name like get_word_count would be less confusing.
The same goes for print_lines.

With these suggested renames, now we can introduce some real printing functions, and make the code more DRY:

if args.words:
    print_words(args.filename)

elif args.lines:
    print_lines(args.filename)

else:
    print_words(args.filename)
    print_lines(args.filename)


It won't get much DRY-er than this, and I don't think it needs to.