Click: No such option

Question:

I am trying to alter an existing framework having 18 flags to have 2 more flags. Every single time I try to use the flag, it says that the flag is not existing.

class Config(object):
    def __init__(self):
        self.cv_data: Optional[List[pd.DataFrame]] = None
        self.cv_labels: Optional[pd.DataFrame] = None
        self.train_data: Optional[List[pd.DataFrame]] = None
        self.train_labels: Optional[pd.DataFrame] = None
        self.test_data: Optional[List[pd.DataFrame]] = None
        self.test_labels: Optional[pd.DataFrame] = None
        self.classes: Optional[Set[int]] = None
        self.num_classes: Optional[int] = None
        self.ts_length: Optional[int] = None
        self.variate: Optional[int] = None
        self.strategy: Optional[str] = None
        self.timestamps: Optional[List[int]] = None
        self.folds: Optional[int] = None
        self.target_class: Optional[int] = None
        self.output: Optional[click.File] = None
        self.java: Optional[bool] = None
        self.file: Optional[click.File] = None
        self.splits: Optional[dict] = None
        self.make_cv: Optional[bool] = None
        self.realtime: Optional[bool] = None
        self.interval: Optional[int] = None


pass_config = click.make_pass_decorator(Config, ensure=True)


@click.group()
@click.option('-i', '--input-cv-file', type=click.Path(exists=True, dir_okay=False),
              help='Input CSV data file for cross-validation.')
@click.option('-t', '--train-file', type=click.Path(exists=True, dir_okay=False),
              help='Train CSV data file.')
@click.option('-e', '--test-file', type=click.Path(exists=True, dir_okay=False),
              help='Test CSV data file.')
@click.option('-s', '--separator', type=click.STRING, default=',', show_default=True,
              help='Separator of the data files.')
@click.option('-d', '--class-idx', type=click.IntRange(min=0),
              help='Class column index of the data files.')
@click.option('-h', '--class-header', type=click.STRING,
              help='Class column header of the data files.')
@click.option('-z', '--zero-replacement', type=click.FLOAT, default=1e-10, show_default=True,
              help='Zero values replacement.')
@click.option('-r', '--reduction', type=click.IntRange(min=1), default=1, show_default=True,
              help='Dimensionality reduction.')
@click.option('-p', '--percentage', type=click.FloatRange(min=0, max=1), multiple=True,
              help='Time-series percentage to be used for early prediction (multiple values can be given).')
@click.option('-v', '--variate', type=click.IntRange(min=1), default=1, show_default=True,
              help='Number of series (attributes) per example.')
@click.option('-g', '--strategy', type=click.Choice(['merge', 'vote', 'normal'], case_sensitive=False),
              help='Multi-variate training strategy.')
@click.option('-f', '--folds', type=click.IntRange(min=2), default=5, show_default=True,
              help='Number of folds for cross-validation.')
@click.option('-c', '--target-class', type=click.INT,
              help='Target class for computing counts. -1 stands for f1 for each class')
@click.option('--java', is_flag=True,
              help='Algorithm implementation in java')
@click.option('--cplus', is_flag=True,
              help='Algorithm implementation in C++')
@click.option('--splits', type=click.Path(exists=True, dir_okay=False), help='Provided fold-indices file'
              )
@click.option('--make-cv', is_flag=True,
              help='If the dataset is divided and cross-validation is wanted'
              )
@click.option('-o', '--output', type=click.File(mode='w'), default='-', required=False,
              help='Results file (if not provided results shall be printed in the standard output).')
@click.option('--realtime', is_flag=True, help="Activates the realtime mode in which the input of the data happens in real time. Requires parameter --interval")
@click.option('--interval', default=100, help="Interval after how many milliseconds there will be a new datapoint")

@pass_config
def cli(config: Config,
        input_cv_file: click.Path,
        train_file: click.Path,
        test_file: click.Path,
        separator: str,
        class_idx: int,
        class_header: str,
        zero_replacement: float,
        reduction: int,
        percentage: List[float],
        variate: int,
        strategy: click.Choice,
        folds: int,
        target_class: int,
        java: bool,
        cplus: bool,
        splits: click.Path,
        output: click.File,
        make_cv: bool,
        realtime: bool,
        interval: int,) -> None:
    """
    Library of Early Time-Series Classification algorithms.
    """

    # Store generic parameters
    config.variate = variate
    config.strategy = strategy
    config.folds = folds
    config.target_class = target_class
    config.output = output
    config.java = java
    config.cplus = cplus
    config.splits = None
    config.make_cv = make_cv
    config.interval = interval
    config.realtime = realtime

That is the given code. I tried to "realtime" and "interval".

Every time I try to run the programme using –interval or –realtime, I get the error

Error: no such option: --interval

However, if I run the –help command, the commands are actually listed.

Is there anything I am doing wrong?

Asked By: DORpapst

||

Answers:

click groups with subcommands are picky about argument ordering. Since what you’re attempting to modify is indeed a Click group, you’ll need to pass --interval before the subcommand name.

A simplified example:

import click
import dataclasses


@dataclasses.dataclass
class Config:
    interval: int = 0


pass_config = click.make_pass_decorator(Config, ensure=True)


@click.group()
@click.option('--interval', default=1)
@pass_config
def cli(config, interval):
    config.interval = interval


@cli.command()
@click.option('--toughness', default=8)
@pass_config
def mangle(config, toughness):
    print(f"Mangling at {config.interval} interval and {toughness} toughness")


if __name__ == '__main__':
    cli()

Passing --interval after mangle: no go.

$ python3 so73649041.py mangle --interval=8
Usage: so73649041.py mangle [OPTIONS]
Try 'so73649041.py mangle --help' for help.

Error: No such option: --interval

Passing --interval before mangle: go.

$ python3 so73649041.py --interval=3 mangle
Mangling at 3 interval and 8 toughness

Passing a subcommand’s option before its name: no go.

$ python3 so73649041.py --toughness=7 mangle
Usage: so73649041.py [OPTIONS] COMMAND [ARGS]...
Try 'so73649041.py --help' for help.

Error: No such option: --toughness

Passing everything: go.

$ python3 so73649041.py --interval=42 mangle --toughness=110
Mangling at 42 interval and 110 toughness
Answered By: AKX