Skip to content

User: Introduction to default options

ccc edited this page Nov 5, 2018 · 9 revisions

Run the command with a help option (to get the latest options), the output looks like below, which are effective to all sub-commands:

$ krep --help
Usage: krep [OPTIONS] subcmd ...

Execute the specified sub-commands, which can be listed with the command
"krep help"

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit

  Global file options:
    -w DIR, --working-dir=DIR
                        Set the working directory. default: /home/experi/krep

  Global debugging options:
    -T, --dryrun        try running the command without actual changes to the
                        local repository and the remote server
    -v, --verbose       set repeatedly to output debugging info

  Global other options:
    --force             force to execute the operations
    --inject-option=INJECT_OPTION
                        extra options passed to running sub-command with the
                        format group:[--]option[=value]

Once sub-comand is invoked with the option --help, the output will mix up the global options and the one provided or enabled by the sub-command.

Working directory

Option working-dir indicates the directory to run the program. By default it's current directory.

An suppressed option relative-dir can be used in configuration files to conjunct with working-dir to build up the final working directory, which can be used with the sub-command to wrap other sub-commands.

Debugging

krep provides general debugging method with verbose prints and dry-run for options.

Option verbose is increased to raise the debugging level. The default is only to output the error. More verbose options are used, more prints can be seem (Maximum level is four to reach the highest level).

$ krep -vvvvv --verbose --verbose

And the default verbose can be defined with the environmental variable KREP_VERBOSE. With it, the startup of the program can also be debugged even before the options from the command line are handled.

Option dry-run tries to process the sub-command but won't execute the command actually. It can be used to demonstrate the uncertain execution.

Configuration files

User can configure to preset the options in two configuration files in git-config format or XML one:

  • /etc/default/krepconfig
  • ~/.krepconfig

/etc/default/krepconfig can be treated as the global variables, for example, to contain the job number, which depends on the CPU core number.

job = 8

~/.krepconfig can be used as the customized one handled by each user for the daily work. In the case, remote server can be included:

remote = user@remote_server

Injected options

As krep implements to load options dynamically from the imported Python classes, each sub-command could have quite different options. If a sub-command like batch wrapped other sub-commands, it's not possible to expose all of their options. injection-option is the option supported by such sub-commands to accept the options and pass the option values to the wrapped sub-command like the normal one.

$ krep batch --inject-option "offsite" --inject-option "job=4"

What's more, the injection option can be categorized with the sub-command name. If the option has the name, it'll be effective to the sub-command only, for example:

$ krep batch --inject-option "git-p:offsite" --inject-option "repo:job=4"

Extra options

Sub-command might invoke external executable, which supports its own options. It's hard to map all executable options as the sub-command options if the executable provides lots of options. An alternative is to provide a specific option to bypass these sub-command options.

extra-option is the one to support the function. krep git-p --help shows the output:

Usage: krep git-p [options] ...

...

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit

...

  Global other options:
    --force             force to execute the operations
    --offsite           not to fetch from the network and work offline
    --extra-option=EXTRA_OPTION
                        extra options in internal group with prefix. The
                        format is like "inject-option":

                        Gerrit options for create-project:
                          gerrit-cp:branch          initial branch name
                          gerrit-cp:empty-commit    to create initial empty commit
                          gerrit-cp:description     description of project
                          gerrit-cp:owner           owner(s) of the project
                          gerrit-cp:parent          parent project
...

Because git-p supports to access gerrit server, which supports create-project as the public sub-command to create gerrit project (git repository), the extra options of gerrit-cp series are implemented to provide the options to gerrit.

What's more, the series of extra options for git have been implemented to avoid a mass of codes to update for an option - It's an alternative to simplify the code.

Option priority

Several options are mentioned. User will handle several options from different source:

  • Two configuration files
  • Injection options
  • options from command line

How will the program run if options are provided more than once and the options aren't set to append? The priority should be defined for the purpose, which has the order below:

  1. Command line
  2. Injected options
  3. ~/.krepconfig
  4. /etc/default/krepconfig

The options provided by the source with lower priority will be overwritten by the higher one. It means the options in user configuration file ~/.krepconfig will replace the ones in /etc/default/krepconfig. And the options from the command line will overwrite the ones in the configuration files.