Ask for a password with OptParse

Robert Kern robert.kern at gmail.com
Tue Jul 3 19:28:40 EDT 2007


Mariano Mara wrote:
> Hi everyone.
> I'm building a script with optparse. One of the parameters will be a 
> password.
> How can I code the optparse to accept/handle/format the password so that 
> the user does not have to write it in plain/visible text in the terminal?

You wouldn't ask for the password with optparse. optparse just parses
command-line options.

If you must provide a command-line argument to accept a password, then check if
the option value is None (i.e. the user did not provide it), then prompt the
user using getpass.getpass(). That function will not print the user's input to
the screen.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list