Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Password / Masked Input

For passwords, API tokens, and other sensitive values, you want to hide the user’s typed input so it doesn’t appear on screen. ArgMojo’s .password() builder method does exactly this — equivalent to Click’s hide_input=True or Python’s getpass.getpass().

Naming note: The method is named .password() following HTML’s <input type="password"> convention, where the word “password” universally signals “hide the typed characters”. Click uses hide_input=True (describing the behaviour); ArgMojo uses .password() (describing the use case) for brevity and instant recognition.

Basic Usage

from argmojo import Command, Argument

def main() raises:
    var command = Command("login", "Authenticate with the server")
    command.add_argument(
        Argument("username", help="Your username").long["username"]().short["u"]().required()
    )
    command.add_argument(
        Argument("password", help="Your password").long["password"]().short["p"]().password()
    )

    var result = command.parse()
    var user = result.get_string("username")
    var pass_ = result.get_string("password")
$ ./login --username alice
Your password: <hidden>

The .password() builder method:

  1. Implies .prompt() — if prompting is not already enabled, .password() enables it automatically.
  2. Disables terminal echo — on POSIX systems (macOS, Linux), terminal echo is suppressed via tcsetattr(3) while the user types, then re-enabled afterwards.
  3. Falls back gracefully — if stdin is not a terminal (piped input, CI, /dev/null), echo control silently returns False and prompting stops via the normal EOF-handling path. Defaults are applied as usual.

Custom Prompt Text

Combine .password() with .prompt["text"]() to customise the prompt message:

command.add_argument(
    Argument("token", help="API token")
    .long["token"]()
    .prompt["Enter your API token"]()
    .password()
)
Enter your API token: ••••••••

The order of .prompt["text"]() and .password() does not matter — both produce the same result.

Restrictions

.password() can only be used on value-taking arguments (named options or positionals). It cannot be combined with:

  • .flag() — flags are boolean and don’t read input.
  • .count() — count arguments are incremental and don’t read input.

Attempting to register a .password() argument on a flag or count raises a registration-time error.

Non-Interactive Use

When stdin is not a terminal, the echo-control calls return False (harmless), and input() raises on EOF. ArgMojo catches the exception and stops prompting gracefully — exactly the same behaviour as regular .prompt() arguments.

To bypass the prompt entirely, provide the value on the command line:

./login --username alice --password s3cret