1

Is it possible to define argument dependencies for the Rust CLAP crate in the following way:

  • The first argument is a mandatory thing which defines the type of struct to be created
  • The arguments following this define the values of the struct fields

For example, we might have two structs:

pub struct FirstStruct {
    pub field1: String,
    pub field2: i64,
}

pub struct SecondStruct {
    pub field1: String,
    pub field4: String,
    pub field5: i32,
}

The command line interface to this would look something like this:

$ ./struct-creator --type FirstStruct --field1 hi --field2 -20
$ ./struct-creator --type SecondStruct --field1 hello --field4 world --field5 100

It is possible to define a subcommand with CLAP. However, this doesn't seem like the correct approach, because a command cannot be in the format --command, instead it is just command.

This lead me to conclude that I don't want to format the arguments using a "CLAP command", instead they should all be "CLAP args".

Thus far I have been trying to do this with the derive interface, but using the builder would also be acceptable. I don't know if one offers more flexibility than the other, or if they can do exactly the same things.

The trivial solution would be to make all of the args optional and then just write some runtime logic to explicitly check for valid combinations. But this results in extensive combinatorics logic, which is painful to write, and maintain.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • IMHO, even from a user perspective this is really hard to use, you might consider accepting file path as an argument where the file contains these definitions as json or some other data format. – Ömer Erden Jul 08 '23 at 13:30
  • @ÖmerErden That makes sense as a suggestion, but then it isn't easy for the user to change values, other than by having some other thing create the file first... which just repeats the same problem because that "thing" has to take the same sets of arguments. – FreelanceConsultant Jul 08 '23 at 13:37
  • @ÖmerErden FYI the intended use case for this is as part of a script, or just something a user runs manually from the command line. – FreelanceConsultant Jul 08 '23 at 13:38

0 Answers0