Ask for a password with OptParse

John Machin sjmachin at
Wed Jul 4 01:48:16 CEST 2007

On Jul 4, 8:50 am, Mariano Mara <m... at> 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?
> TIA,
> Mariano.

If you insist on those "requirements", you don't need to do anything
special in the call to optparse. However you do need to (1) do some
experimentation to determine the set of non-printing characters that
can be entered at the command line and passed though to the
application without being changed or "interpreted" by the shell(s) or
Python or optparse (2) restrict the password-issuer so that passwords
must be composed only from the so-determined set (3) buy some ear
plugs to block out the screaming from the users ...

Alternatively, change the requirements to something like: (1) check if
there is an interactive console/tty [if not, abort] (2) use
getpass.getpass to prompt for a password.

More information about the Python-list mailing list