liveBook · Manning

Positional parameter

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A command-line parameter whose meaning is determined by its position in the argument sequence rather than by an option name.

A positional parameter is a command-line argument whose meaning comes from its position rather than an option name such as -n or --name. With argparse, positional parameters are usually required and appear without leading dashes. section 1.13 section 1.8

Define positional parameters with argparse and shape their behavior explicitly:

  • Give the parameter a descriptive name, such as word, text, letter, or file, instead of leaving the template name positional. section 2.3.1 section 4.4.1
  • Declare the parameter with parser.add_argument('word', metavar='word', help='Word'); argparse then exposes the value as args.word. section 2.3.1 section 2.1.4
  • Define multiple positional parameters in order: the first declaration receives the first positional value, while options may be declared anywhere relative to them. section 5.5.1
  • Use nargs='+' for one or more values and nargs='*' for zero or more; the parsed result is a list when multiple values are accepted. section 3.1 section 6.1.1
  • Combine positional parameters with optional flags when a command needs both required input and configurable behavior. section 3.2 section 8.3.1

A word argument

A parser can require one word and use it in the program: parser.add_argument('word', metavar='word', help='A word'). Running ./crowsnest.py narwhal supplies args.word with "narwhal"; running the program without it produces usage documentation and an error. A command such as ./crowsnest.py narwhal --file alternate.txt still works because positional arguments need not precede options. section 2.1.4 section 5.5.1

Several values

For a picnic command, parser.add_argument('item', nargs='+', help='Item(s) to bring') collects ./picnic.py salad chips cupcakes as ['salad', 'chips', 'cupcakes']. The program can count, sort, and format those values as one, two, or three-or-more items. section 3.1 section 3.3 section 3.2

The argparse module is a standard module that is always installed with Python. It’s widely used because it can save us so much time in parsing and validating the arguments to our programs. You’ll be using argparse in every program for this book, and you’ll learn how you can use it to convert text to numbers, to validate and open files, and much more. There are so many options that I created a Python program called new.py that will help you start writing new Python programs that use argparse.

Browse the argparse overview to see how positional arguments, options, flags, validation, and generated help fit together. section 1.13

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains naming, ordering, requiredness, nargs='+' and nargs='*', lists, help output, and validation, without descending into argparse internals.
Examples
Demonstrates word, text, letter, item, and file arguments through greeting, picnic, lookup, file-reading, and string-replacement programs.
What it assumes
Assumes basic Python and terminal use while teaching argument parsing, help output, validation, and parsed values incrementally.
What you can do after
Build command-line interfaces that parse, validate, document, and consume one or more positional values.
How it connects
Connects positional parameters with argparse, optional arguments, flags, lists, file inputs, validation, and generated usage documentation.

Also explained in

Compare how 5 books explain positional parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.