Add support for subcommands

finchie
Nov 20, 2023, 12:34 PM
NSWIJUHGFP34MVP46XYEKFEWQN6TEGDFSX3A7PIPVDOORTE5CB5QC

Dependencies

  • [2] MT5RA7VY Increase sidebar density
  • [3] ZVDBFCW7 Display arguments when input is invalid
  • [4] MIY7QPYK Refactor argument handling into a separate file
  • [5] C73UJ7ZY Create simple `xilem_html` demo
  • [6] YTW5RB26 Refactor argument type handling into enum
  • [7] BMG4FSHN Add basic `clap` support
  • [8] LEJN3E4Q Generate more semantic HTML

Change contents

  • replacement in src/main.rs at line 46
    [3.408][3.408:439]()
    arg::args_view(state),
    [3.921]
    [3.1003]
    arg::sidebar(&state),
  • replacement in src/cli.rs at line 1
    [3.503][3.504:522]()
    use clap::Parser;
    [3.503]
    [3.522]
    use clap::{Parser, Subcommand};
  • edit in src/cli.rs at line 18
    [3.865]
    [3.865]
    // Subcommand
    #[command(subcommand)]
    subcommand: Option<Subcmd>,
    }
    #[derive(Subcommand, Debug, Clone, Copy)]
    pub enum Subcmd {
    /// First subcommand
    ///
    /// This explanation is also quite long.
    /// It takes up multiple lines
    One {
    #[arg(long)]
    times: u32,
    },
    /// Second subcommand
    ///
    /// Another, even longer explanation.
    /// Would it not be crazy
    /// if this took up 3 lines??
    Two {
    #[arg(short = 'a', long)]
    amount: i64,
    },
  • replacement in src/arg.rs at line 3
    [3.672][3.0:40]()
    use clap::{Arg, ArgMatches, ValueHint};
    [3.672]
    [2.0]
    use clap::{Arg, ArgMatches, Command, ValueHint};
  • replacement in src/arg.rs at line 125
    [3.2465][3.2465:2537](),[3.2537][3.176:243]()
    fn source(state: &AppState, arg: &Arg) -> impl View<Arg> + ViewMarker {
    el::span(if let Ok(arg_matches) = state.arg_matches.as_ref() {
    [3.2465]
    [3.2608]
    fn source(arg_matches: Option<&ArgMatches>, arg: &Arg) -> impl View<Arg> + ViewMarker {
    el::span(if let Some(arg_matches) = arg_matches.as_ref() {
  • replacement in src/arg.rs at line 162
    [3.720][3.3013:3093](),[3.3013][3.3013:3093]()
    fn matched_values(state: &AppState, arg: &Arg) -> impl View<Arg> + ViewMarker {
    [3.720]
    [3.183]
    fn matched_values(arg_matches: Option<&ArgMatches>, arg: &Arg) -> impl View<Arg> + ViewMarker {
  • replacement in src/arg.rs at line 164
    [3.232][3.232:310]()
    let arg_matches = state.arg_matches.as_ref().unwrap_or(&default_matches);
    [3.232]
    [3.310]
    let arg_matches = arg_matches.unwrap_or(&default_matches);
  • replacement in src/arg.rs at line 189
    [3.3778][3.3778:3848]()
    fn item(state: &AppState, arg: &Arg) -> impl View<Arg> + ViewMarker {
    [3.3778]
    [3.721]
    fn item(arg_matches: Option<&ArgMatches>, arg: &Arg) -> impl View<Arg> + ViewMarker {
  • replacement in src/arg.rs at line 201
    [3.4512][3.4512:4540]()
    source(state, arg),
    [3.4512]
    [3.4540]
    source(arg_matches, arg),
  • replacement in src/arg.rs at line 205
    [3.4672][3.4672:4708]()
    matched_values(state, arg),
    [3.4672]
    [3.4708]
    matched_values(arg_matches, arg),
  • replacement in src/arg.rs at line 210
    [3.4718][3.4718:4795](),[3.4795][3.1064:1077]()
    pub fn args_view(state: &mut AppState) -> impl View<AppState> + ViewMarker {
    el::div(
    [3.4718]
    [3.4838]
    fn args_view(command: &Command, arg_matches: Option<&ArgMatches>) -> impl ViewSequence<AppState> {
    command
    .get_opts()
    .map(|arg| {
    Adapt::new(
    |_state, _thunk| {
    // TODO: actually re-compute this value
    MessageResult::Nop
    },
    item(arg_matches, arg),
    )
    })
    .collect::<Vec<_>>()
    }
    pub fn sidebar(state: &AppState) -> impl View<AppState> + ViewMarker {
    let arg_matches = state.arg_matches.as_ref().ok();
    el::div((
    args_view(&state.command, arg_matches),
  • replacement in src/arg.rs at line 231
    [3.4873][3.4873:5157]()
    .get_opts()
    .map(|arg| {
    Adapt::new(
    |_state, _thunk| {
    // TODO: actually re-compute this value
    MessageResult::Nop
    },
    item(state, arg),
    [3.4873]
    [3.5157]
    .get_subcommands()
    .map(|subcommand| {
    args_view(
    subcommand,
    arg_matches.and_then(|arg_matches| {
    arg_matches.subcommand_matches(subcommand.get_name())
    }),
  • replacement in src/arg.rs at line 241
    [3.1112][3.1112:1118]()
    )
    [3.1112]
    [3.5262]
    ))