CustomCommand

class gratools.CustomCommand(*args, **kwargs)[source]

Bases: Command

Custom Command class that applies global context settings (for help formatting) and prepends a specific header to the help message of individual commands.

Attributes Summary

allow_extra_args

the default for the Context.allow_extra_args flag.

allow_interspersed_args

the default for the Context.allow_interspersed_args flag.

ignore_unknown_options

the default for the Context.ignore_unknown_options flag.

Methods Summary

__call__(*args, **kwargs)

Alias for main().

collect_usage_pieces(ctx)

Returns all the pieces that go into the usage line and returns it as a list of strings.

format_aliases(ctx, formatter)

format_constraints(ctx, formatter)

format_epilog(ctx, formatter)

Writes the epilog into the formatter if it exists.

format_help(ctx, formatter)

Writes the help into the formatter if it exists.

format_help_text(ctx, formatter)

Writes the help text to the formatter if it exists.

format_options(ctx, formatter)

Writes all the options into the formatter if they exist.

format_params(ctx, formatter)

format_usage(ctx, formatter)

Writes the usage line into the formatter.

get_argument_help_record(arg, ctx)

get_arguments_help_section(ctx)

get_default_option_group(ctx[, ...])

Return an OptionGroup instance for the options not explicitly assigned to an option group, eventually including the --help option.

get_help(ctx)

Overrides the default help generation to prepend a custom header.

get_help_option(ctx)

Returns the help option object.

get_help_option_names(ctx)

Returns the names for the help option.

get_normalized_epilog()

get_param_by_name(name)

get_params(ctx)

get_params_by_name(names)

get_short_help_str([limit])

Gets short help for the command or makes it by shortening the long help string.

get_ungrouped_options(ctx)

Return options not explicitly assigned to an option group (eventually including the --help option), i.e. options that will be part of the "default option group".

get_usage(ctx)

Formats the usage line into a string and returns it.

invoke(ctx)

Mesure et affiche le temps d'exécution autour de l’appel réel.

main([args, prog_name, complete_var, ...])

This is the way to invoke a script with all the bells and whistles as a command line application.

make_context(info_name, args[, parent])

This function when given an info name and arguments will kick off the parsing and create a new Context.

make_option_group_help_section(group, ctx)

Return a HelpSection for an OptionGroup, i.e. an object containing the title, the optional description and the options' definitions for this option group.

make_parser(ctx)

Creates the underlying option parser for this command.

must_align_option_groups(ctx[, default])

Return True if the help sections of all options groups should have their columns aligned.

must_show_constraints(ctx)

parse_args(ctx, args)

shell_complete(ctx, incomplete)

Return a list of completions for the incomplete value.

to_info_dict(ctx)

Attributes Documentation

allow_extra_args = False

the default for the Context.allow_extra_args flag.

allow_interspersed_args = True

the default for the Context.allow_interspersed_args flag.

ignore_unknown_options = False

the default for the Context.ignore_unknown_options flag.

Methods Documentation

__call__(*args, **kwargs)

Alias for main().

Parameters:
Return type:

Any

collect_usage_pieces(ctx)

Returns all the pieces that go into the usage line and returns it as a list of strings.

Parameters:

ctx (Context)

Return type:

list[str]

format_aliases(ctx, formatter)
Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_constraints(ctx, formatter)
Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_epilog(ctx, formatter)

Writes the epilog into the formatter if it exists.

Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_help(ctx, formatter)

Writes the help into the formatter if it exists.

This is a low-level method called by get_help().

This calls the following methods:

Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_help_text(ctx, formatter)

Writes the help text to the formatter if it exists.

Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_options(ctx, formatter)

Writes all the options into the formatter if they exist.

Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_params(ctx, formatter)
Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

format_usage(ctx, formatter)

Writes the usage line into the formatter.

This is a low-level method called by get_usage().

Parameters:
  • ctx (Context)

  • formatter (HelpFormatter)

Return type:

None

get_argument_help_record(arg, ctx)
Parameters:
  • arg (Argument)

  • ctx (Context)

Return type:

Tuple[str, str]

get_arguments_help_section(ctx)
Parameters:

ctx (Context)

Return type:

HelpSection | None

get_default_option_group(ctx, is_the_only_visible_option_group=False)

Return an OptionGroup instance for the options not explicitly assigned to an option group, eventually including the --help option.

Added in version 0.8.0.

Parameters:
  • ctx (Context)

  • is_the_only_visible_option_group (bool)

Return type:

OptionGroup

get_help(ctx)[source]

Overrides the default help generation to prepend a custom header.

The header is printed directly to the console using shared_console before the standard help text is generated and returned.

Parameters:

ctx (click.Context) – The current Click context.

Returns:

str – The formatted help text, including the prepended header.

Return type:

str

get_help_option(ctx)

Returns the help option object.

Skipped if add_help_option is False.

Changed in version 8.1.8: The help option is now cached to avoid creating it multiple times.

Parameters:

ctx (Context)

Return type:

Option | None

get_help_option_names(ctx)

Returns the names for the help option.

Parameters:

ctx (Context)

Return type:

list[str]

get_normalized_epilog()
Return type:

str

get_param_by_name(name)
Parameters:

name (str)

Return type:

Parameter

get_params(ctx)
Parameters:

ctx (Context)

Return type:

list[Parameter]

get_params_by_name(names)
Parameters:

names (Iterable[str])

Return type:

Sequence[Parameter]

get_short_help_str(limit=45)

Gets short help for the command or makes it by shortening the long help string.

Parameters:

limit (int)

Return type:

str

get_ungrouped_options(ctx)

Return options not explicitly assigned to an option group (eventually including the --help option), i.e. options that will be part of the “default option group”.

Parameters:

ctx (Context)

Return type:

Sequence[Option]

get_usage(ctx)

Formats the usage line into a string and returns it.

Calls format_usage() internally.

Parameters:

ctx (Context)

Return type:

str

invoke(ctx)[source]

Mesure et affiche le temps d’exécution autour de l’appel réel.

main(args=None, prog_name=None, complete_var=None, standalone_mode=True, windows_expand_args=True, **extra)

This is the way to invoke a script with all the bells and whistles as a command line application. This will always terminate the application after a call. If this is not wanted, SystemExit needs to be caught.

This method is also available by directly calling the instance of a Command.

Parameters:
  • args (Sequence[str] | None) – the arguments that should be used for parsing. If not provided, sys.argv[1:] is used.

  • prog_name (str | None) – the program name that should be used. By default the program name is constructed by taking the file name from sys.argv[0].

  • complete_var (str | None) – the environment variable that controls the bash completion support. The default is "_<prog_name>_COMPLETE" with prog_name in uppercase.

  • standalone_mode (bool) – the default behavior is to invoke the script in standalone mode. Click will then handle exceptions and convert them into error messages and the function will never return but shut down the interpreter. If this is set to False they will be propagated to the caller and the return value of this function is the return value of invoke().

  • windows_expand_args (bool) – Expand glob patterns, user dir, and env vars in command line args on Windows.

  • extra (Any) – extra keyword arguments are forwarded to the context constructor. See Context for more information.

Return type:

Any

Changed in version 8.0.1: Added the windows_expand_args parameter to allow disabling command line arg expansion on Windows.

Changed in version 8.0: When taking arguments from sys.argv on Windows, glob patterns, user dir, and env vars are expanded.

Changed in version 3.0: Added the standalone_mode parameter.

make_context(info_name, args, parent=None, **extra)

This function when given an info name and arguments will kick off the parsing and create a new Context. It does not invoke the actual command callback though.

To quickly customize the context class used without overriding this method, set the context_class attribute.

Parameters:
  • info_name (str | None) – the info name for this invocation. Generally this is the most descriptive name for the script or command. For the toplevel script it’s usually the name of the script, for commands below it’s the name of the command.

  • args (list[str]) – the arguments to parse as list of strings.

  • parent (Context | None) – the parent context if available.

  • extra (Any) – extra keyword arguments forwarded to the context constructor.

Return type:

Context

Changed in version 8.0: Added the context_class attribute.

make_option_group_help_section(group, ctx)

Return a HelpSection for an OptionGroup, i.e. an object containing the title, the optional description and the options’ definitions for this option group.

Added in version 0.8.0.

Parameters:
  • group (OptionGroup)

  • ctx (Context)

Return type:

HelpSection

make_parser(ctx)

Creates the underlying option parser for this command.

Parameters:

ctx (Context)

Return type:

_OptionParser

must_align_option_groups(ctx, default=True)

Return True if the help sections of all options groups should have their columns aligned.

Added in version 0.8.0.

Parameters:
  • ctx (Context | None)

  • default (bool)

Return type:

bool

must_show_constraints(ctx)
Parameters:

ctx (Context)

Return type:

bool

parse_args(ctx, args)
Parameters:
Return type:

List[str]

shell_complete(ctx, incomplete)

Return a list of completions for the incomplete value. Looks at the names of options and chained multi-commands.

Any command could be part of a chained multi-command, so sibling commands are valid at any point during command completion.

Parameters:
  • ctx (Context) – Invocation context for this command.

  • incomplete (str) – Value being completed. May be empty.

Return type:

list[CompletionItem]

Added in version 8.0.

to_info_dict(ctx)
Parameters:

ctx (Context)

Return type:

dict[str, Any]