# How can I make this command line utility better?

I have been developing a capitalizer in Python. This is actually a script that an end user will type at their terminal/command prompt. I have made this script to:

1. Taking the first argument as a file name and process it.
2. Don't process words that have 3 or less alphabet.

Here's some code:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from os import linesep
from string import punctuation
from sys import argv

script, givenfile = argv

with open(givenfile) as file:
# List to store the capitalised lines.
lines = []
for line in file:
# Split words by spaces.
words = line.split()
for i, word in enumerate(words):
if len(word.strip(punctuation)) > 3:
# Capitalise and replace words longer than 3 letters (counts
# without punctuation).
if word != word.upper():
words[i] = word.capitalize()
# Join the capitalised words with spaces.
lines.append(' '.join(words))
# Join the capitalised lines by the line separator.
capitalised = linesep.join(lines)

print(capitalised)


A lot more features still have to be added but this script does the stuff it is made for.

### What I want (from you):

I want this script to be emerge as a command line utility. So,

1. How can I make this a better command line utility?
2. Could I have written in a better way?
3. Can this script be faster?
4. Are there any flaws?
5. Other than GitHub, how can I make it available to the world (so that it is easier to install for end users)? because GitHub is for developers.

Please also post links to make my script and me even more mature.

-
"because GitHub is for developers." Non-developers tend to not use command line scripts... –  Corbin Sep 29 '12 at 7:00

How can you make this a better command-line utility?

Along with using argparse (which is a great suggestion), I would suggest adding options for reading files from stdin, and maybe an option to write to somewhere other than stdout (to a file, for example).

Could it be written better / can it be faster?

Of course! There's always room for improvement. The biggest performance issue I can see here is that you append the output to the lines list, then output at the very end. The downside to this is that you must hold the entire output in memory before you return. If the file is large, it will be slow due to all the allocation/garbage-collection, and in extreme cases you could run out of memory!

Instead of appending to lines and joining at the end, I'd suggest you replace this line:

lines.append(' '.join(words))


with

print ' '.join(words)  # probably needs replaced with something that preserves whitespace.


Then delete all lines that refer to lines:

lines = [] and capitalised = linesep.join(lines) and print(capitalised)

With this, and allowing for input from stdin, you could use it in unix pipelines, like so: cat somefile | capitalizer | grep 'Foo' (or something like that), and you'll see output immediately (as opposed to when the entire file is processed).

Is there an easier way for end-users to install this?

Yes, kind-of... as long as your users are not afraid of the command-line (which I assume, since this is a command-line program), you can publish this to PyPi, then it can be installed with tools like PIP and easy_install.

Here's a simple tutorial for getting started with PyPi

When setup properly, installation can be a simple as:

easy_install capitalizer or pip install capitalizer

Finally, discovery is a bit better than just github, as users can find your app with tools like the PyPi website, and crate.io.

-

### As a Command Line Utility

The Argparse module is designed to facilitate the creation of command line utilities. It makes processing command line arguments much easier, and will even automatically display usage information if the user incorrectly enters arguments.

### Potential Flaws

Your program does not preserve whitespace. This could be by design but I suspect it is not.
Example:

the      quick      brown  fox


will become

the Quick Brown Fox

-