Knowledgebase

Handling Command-Line Arguments Print

  • python, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Making scripts flexible.

WHY USE ARGUMENTS

So the same script serves several cases without editing.

WHAT TO USE

Python's argument parsing facilities.

Not manual inspection of the raw arguments.

WHAT THEY PROVIDE

Named options and positional arguments Types and validation Default values Automatic help output

THE HELP OUTPUT

Worth it alone.

A script that explains itself is usable by someone else.

WHAT TO MAKE ARGUMENTS

Anything that varies: input paths, output paths, dates, modes

WHAT NOT TO HARD-CODE

File paths Dates Anything environment-specific

WHAT TO PROVIDE

Sensible defaults, so the common case needs no arguments.

WHAT TO INCLUDE FOR ANYTHING DESTRUCTIVE

A mode that reports what would happen without doing it.

WHY

It lets you verify before committing.

WHAT TO VALIDATE

Every argument, before starting work.

WHAT TO RETURN

A meaningful exit status, so callers can tell whether it succeeded.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot