Skip to content

Developer: Introduce to option extensions

ccc edited this page Dec 5, 2018 · 1 revision

Python provides several ways to accept the command line options like optparse and argparse. it can almost handle all options except that was supported by krep:

  • no or not prefix for an option to indicate the negative value
  • Overrided options from the configuration files
  • Injection options from the options of batch sub-command

The priority of options from configuration files, command line, and the injection options can refer to User: Introduction to option extensions.

Some demonstrations could be:

# Inject the offiste for the sub-command repo-mirror
$ krep batch -f all.xml -u aosp --injection-option "--offsite"
# Suppress the tag pushing
$ krep batch -f all.xml -u git-repo --injection-option "--all" --injection-option "--no-tags"
# Suppress the tag pushing
$ krep git-b --git https://gerrit.googlesource.com/git-repo --repo-create --no-description

Because of all the new changes, a new implemented options.py was added to inherit from optparse.OptionParser and optparse.Values.

The extensions of optparse.OptionParser includes:

  • Add an implicited option into a "Pseduo options" group if an opposite option is provided and it's not an existed option registered by any of the sub-command
  • Suppress the option and set the default value if provided for the sub-command as a sub-class of other sub-command to remove some of the options. With it, the sub-command can be more simple to adjust the option as a new sub-command. For example, The difference of repo_mirror_subcmd and repo_subcmd

And the extenions of optparse.Values have:

  • Secure the value and convert to the right types (according to the option type if provided) for ensure_value
  • Combine two option values with/without the override
  • Generate the different option values comparing with others

With the extensions, more powerful functions could be implementated by krep.

Options for optparse

To avoid duplicatedly coding the support options for similar functions of each, sub-command, krep will attempt to load the existent options from the imported classes:

  • options will be appended to optparse if class has method options
  • extra options will be appended if class has attribute extra_items

If an external class need to export options, it has to implement its own options method or extra_items list.

sub-command options

The option follows the standard function of optparse, which supports to add option group and then add the options.

    @staticmethod
    def options(optparse):
        pass

The option will only be showed when the class is imported by the sub-command. Different sub-commands show different options. For example, Sub-command batch shows few options than repo as batch imported few classes.

Injection-options and extra-options

injection-options will be showed once the sub-command overrides the method support_inject.

    def support_inject(self):  # pylint: disable=W0613
        return True 

But extra-options can be showed when extra_items is defined or support_extra returns True like support_inject.

Opposite options

Generally, there's no opposite option defined explicitly. krep will construct the oppsite options when an option from the command-line starts with no or not after a successful checkup of the supported options.