I have two modes that my Funge98 interpreter can be in: normal and quiet, and each should have their own sets of arguments. Originally I put them everything in one giant argument struct (with quiet mode as -q) and ignored arguments when they didn't apply. This made it a bit unintuitive, so I tried separating them into struct-like enum variants, so that they could have separate arguments. This got annoying, since it made it so you had to type run before the file to run it in normal. After that, I tried adding #[command(name="")] and #[command(name="-q")] to the two enum variants, but this made it impossible to use the normal mode. Is there a way to make a subcommand act as a default for when nothing is typed before the first argument without doing the name="" hack?
#clap subcommands as flags (or the lack thereof)
5 messages · Page 1 of 1 (latest)
#[derive(clap::Parser)]
pub enum Arguments {
/// run the interpreter in tui mode
#[command(name="")]
Run {
/// start interpretation paused
#[arg(short, long)]
paused: bool,
/// immediately jump to this many ticks into the sim
#[arg(short, long)]
jump: Option<u32>,
/// start on the first non-# line
#[arg(short, long)]
script: bool,
/// Target file
file: String
},
/// run the interpreter in quiet mode
#[command(name="-q")]
RunQuiet {
/// print out the stack(s) after ending
#[arg(short='l', long)]
print_stack: bool,
/// end interpreting early
#[arg(short, long="max")]
max_ticks: Option<u32>,
/// start on the first non-# line
#[arg(short, long)]
script: bool,
/// Target file
file: String
}
}
this is my current abomination of an argument system
You might be able to convert these to a subcommand but make it optional and default to run if no argument is provided
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
command: Option<Arguments>,
}
#[derive(Subcommand)]
enum Arguments {
Run { ... }
RunQuiet { ... }
}
Although idk if having a flag -q as an actual command is good practice - this forces you to have -q as the very first argument even though flags should be allowed in any order.
Could try going back to your single struct approach but use conflicts_with to tell the user some flags cant be used with quiet mode
oh i think conflicts_with would be exactly what i want actually