Argparse vs Click

Python has a number of command-line interface builder tools. The standard tool which comes includes with python is Argparse. Recently, a new tool called Click which was built off the older standard tool — Optparse — has become a dependency in a number of other python packages including Flask.

Why Click?

Synopsis

Argparse is designed to parse arguments and provide extensive customization of cli help documentation. Click is designed to automatically handle common cli command tasks and quickly build a standard help menu.

Pros & Cons

Argparse: Collects the arguments and requires app to call methods
Click: Automatically calls decorated method with all arguments, missing are None

Argparse: Provides method .error for customization of error messages
Click: Requires monkey patch to click.exceptions.UsageError.show

Argparse: Supports choice of values for strings
Click: Does not support choice of values for strings

Argparse: Supports customized ordering of help commands, options and args
Click: Does not support non-alphabetic ordering of help commands, options and args

Argparse: Requires help paragraphs for commands to be declared
Click: Automatically pulls help from method documentation (including :params fields!)

Argparse: Requires replacement of RawDescriptionHelpFormatter to change help layout
Click: Automatically abridges help documentation to maintain help layout

Argparse: Supports replacement of default help section headers and usage format
Click: Does not support replacement of help section headers or usage format

Argparse: Does not support automatic prompts for missing requirements
Click: Supports automatic prompts for missing requirements

Argparse: Does not support environment variable retrieval
Click: Supports automatic retrieval of environment variables

Argparse: Supports only file opening
Click: Supports file opening and path validation

Argparse: Does not support progress bar display
Click: Supports automatic progress bar display for iterative processes

Evaluation

Click requires that I design my callable commands for it. This leads to lock-in and some strange workarounds to get the formatting correct. I’ll stick with argparse.

Show your support

Clapping shows how much you appreciated Collective Acuity’s story.