Search code examples
rustargumentscommand-line-interfaceclap

Clap arg group containing arguments with different requirements and conflicts


I am trying to write a CLI in Rust with the help of Clap and YAML. My input will require one argument (file path) and one of the flags -s, -r, or -g. The flags -s and -r will require one of the two flags -t and -m, but the flag -g conflicts with both -t and -m. I am trying to configure it so that -g will not be accepted if -t or -m is selected, but it disallows the use of -s or -r with either -t or -m.

How do I configure my YAML file so that I can disallow -gt and -gm but allow (and require) the use of -t or -m with -s or -r?

cli.yml:

name: mfm
version: "0.1.0"
author: Jonathan Marple <elpramnoj@gmail.com>
about: Media file manager written in rust.
args:
    - INPUT:
        help: Sets the input file(s) to use
        required: true
    - scrape:
        short: s
        long: scrape
        help: Scrape information on show/movie
        requires:
            - DB
    - rename:
        short: r
        long: rename
        help: Rename file(s)
        requires:
            - DB
    - generate:
        short: g
        long: generate
        help: Generate folders for file(s)
        conflicts_with:
            - tvdb
            - tmdb
    - tvdb:
        short: t
        long: tvdb
        help: Pull from tvdb
    - tmdb:
        short: m
        long: tmdb
        help: Pull from tmdb
groups:
    - CMD:
        required: true
        args:
            - scrape
            - rename
            - generate
    - DB:
        args:
            - tvdb
            - tmdb

I've also tried marking DB under conflicts_with: but it behaves the same way regardless.


Solution

  • As per Francois' suggestion, I switched to using subcommands. I had to write out my -t and -m flags and their DB group twice, once for each subcommand that uses them. I was trying to avoid that to keep my YAML file clean and less repetitive but functionality is more important.

    Working YAML file:

    name: mfm
    version: "0.1.0"
    author: Jonathan Marple <elpramnoj@gmail.com>
    about: Media file manager written in rust.
    args:
        - INPUT:
            help: Sets the input file(s) to use
            required: true
            min_values: 1
    subcommands:
        - scrape:
            about: Scrape information on show/movie
            args:
                - tvdb:
                    short: t
                    long: tvdb
                    help: Pull from tvdb
                - tmdb:
                    short: m
                    long: tmdb
                    help: Pull from tmdb
            groups:
                - DB:
                    required: true
                    args:
                        - tvdb
                        - tmdb
        - rename:
            about: Rename file(s)
            args:
                - tvdb:
                    short: t
                    long: tvdb
                    help: Pull from tvdb
                - tmdb:
                    short: m
                    long: tmdb
                    help: Pull from tmdb
            groups:
                - DB:
                    required: true
                    args:
                        - tvdb
                        - tmdb
        - generate:
            about: Generate folders for file(s)