@@ -24,7 +24,6 @@ use crate::output::fmt::Stream;
2424use crate :: output:: { fmt:: Colorizer , write_help, Usage } ;
2525use crate :: parser:: { ArgMatcher , ArgMatches , Parser } ;
2626use crate :: util:: ChildGraph ;
27- use crate :: util:: FlatMap ;
2827use crate :: util:: { color:: ColorChoice , Id } ;
2928use crate :: { Error , INTERNAL_ERROR_MSG } ;
3029
@@ -99,7 +98,6 @@ pub struct Command {
9998 g_settings : AppFlags ,
10099 args : MKeyMap ,
101100 subcommands : Vec < Command > ,
102- replacers : FlatMap < Str , Vec < Str > > ,
103101 groups : Vec < ArgGroup > ,
104102 current_help_heading : Option < Str > ,
105103 current_disp_ord : Option < usize > ,
@@ -1924,129 +1922,6 @@ impl Command {
19241922 self
19251923 }
19261924
1927- /// Replaces an argument or subcommand used on the CLI at runtime with other arguments or subcommands.
1928- ///
1929- /// **Note:** This is gated behind [`unstable-replace`](https://github.com/clap-rs/clap/issues/2836)
1930- ///
1931- /// When this method is used, `name` is removed from the CLI, and `target`
1932- /// is inserted in its place. Parsing continues as if the user typed
1933- /// `target` instead of `name`.
1934- ///
1935- /// This can be used to create "shortcuts" for subcommands, or if a
1936- /// particular argument has the semantic meaning of several other specific
1937- /// arguments and values.
1938- ///
1939- /// # Examples
1940- ///
1941- /// We'll start with the "subcommand short" example. In this example, let's
1942- /// assume we have a program with a subcommand `module` which can be invoked
1943- /// via `cmd module`. Now let's also assume `module` also has a subcommand
1944- /// called `install` which can be invoked `cmd module install`. If for some
1945- /// reason users needed to be able to reach `cmd module install` via the
1946- /// short-hand `cmd install`, we'd have several options.
1947- ///
1948- /// We *could* create another sibling subcommand to `module` called
1949- /// `install`, but then we would need to manage another subcommand and manually
1950- /// dispatch to `cmd module install` handling code. This is error prone and
1951- /// tedious.
1952- ///
1953- /// We could instead use [`Command::replace`] so that, when the user types `cmd
1954- /// install`, `clap` will replace `install` with `module install` which will
1955- /// end up getting parsed as if the user typed the entire incantation.
1956- ///
1957- /// ```rust
1958- /// # use clap_builder as clap;
1959- /// # use clap::Command;
1960- /// let m = Command::new("cmd")
1961- /// .subcommand(Command::new("module")
1962- /// .subcommand(Command::new("install")))
1963- /// .replace("install", &["module", "install"])
1964- /// .get_matches_from(vec!["cmd", "install"]);
1965- ///
1966- /// assert!(m.subcommand_matches("module").is_some());
1967- /// assert!(m.subcommand_matches("module").unwrap().subcommand_matches("install").is_some());
1968- /// ```
1969- ///
1970- /// Now let's show an argument example!
1971- ///
1972- /// Let's assume we have an application with two flags `--save-context` and
1973- /// `--save-runtime`. But often users end up needing to do *both* at the
1974- /// same time. We can add a third flag `--save-all` which semantically means
1975- /// the same thing as `cmd --save-context --save-runtime`. To implement that,
1976- /// we have several options.
1977- ///
1978- /// We could create this third argument and manually check if that argument
1979- /// and in our own consumer code handle the fact that both `--save-context`
1980- /// and `--save-runtime` *should* have been used. But again this is error
1981- /// prone and tedious. If we had code relying on checking `--save-context`
1982- /// and we forgot to update that code to *also* check `--save-all` it'd mean
1983- /// an error!
1984- ///
1985- /// Luckily we can use [`Command::replace`] so that when the user types
1986- /// `--save-all`, `clap` will replace that argument with `--save-context
1987- /// --save-runtime`, and parsing will continue like normal. Now all our code
1988- /// that was originally checking for things like `--save-context` doesn't
1989- /// need to change!
1990- ///
1991- /// ```rust
1992- /// # use clap_builder as clap;
1993- /// # use clap::{Command, Arg, ArgAction};
1994- /// let m = Command::new("cmd")
1995- /// .arg(Arg::new("save-context")
1996- /// .long("save-context")
1997- /// .action(ArgAction::SetTrue))
1998- /// .arg(Arg::new("save-runtime")
1999- /// .long("save-runtime")
2000- /// .action(ArgAction::SetTrue))
2001- /// .replace("--save-all", &["--save-context", "--save-runtime"])
2002- /// .get_matches_from(vec!["cmd", "--save-all"]);
2003- ///
2004- /// assert!(m.get_flag("save-context"));
2005- /// assert!(m.get_flag("save-runtime"));
2006- /// ```
2007- ///
2008- /// This can also be used with options, for example if our application with
2009- /// `--save-*` above also had a `--format=TYPE` option. Let's say it
2010- /// accepted `txt` or `json` values. However, when `--save-all` is used,
2011- /// only `--format=json` is allowed, or valid. We could change the example
2012- /// above to enforce this:
2013- ///
2014- /// ```rust
2015- /// # use clap_builder as clap;
2016- /// # use clap::{Command, Arg, ArgAction};
2017- /// let m = Command::new("cmd")
2018- /// .arg(Arg::new("save-context")
2019- /// .long("save-context")
2020- /// .action(ArgAction::SetTrue))
2021- /// .arg(Arg::new("save-runtime")
2022- /// .long("save-runtime")
2023- /// .action(ArgAction::SetTrue))
2024- /// .arg(Arg::new("format")
2025- /// .long("format")
2026- /// .action(ArgAction::Set)
2027- /// .value_parser(["txt", "json"]))
2028- /// .replace("--save-all", &["--save-context", "--save-runtime", "--format=json"])
2029- /// .get_matches_from(vec!["cmd", "--save-all"]);
2030- ///
2031- /// assert!(m.get_flag("save-context"));
2032- /// assert!(m.get_flag("save-runtime"));
2033- /// assert_eq!(m.get_one::<String>("format").unwrap(), "json");
2034- /// ```
2035- ///
2036- /// [`Command::replace`]: Command::replace()
2037- #[ inline]
2038- #[ cfg( feature = "unstable-replace" ) ]
2039- #[ must_use]
2040- pub fn replace (
2041- mut self ,
2042- name : impl Into < Str > ,
2043- target : impl IntoIterator < Item = impl Into < Str > > ,
2044- ) -> Self {
2045- self . replacers
2046- . insert ( name. into ( ) , target. into_iter ( ) . map ( Into :: into) . collect ( ) ) ;
2047- self
2048- }
2049-
20501925 /// Exit gracefully if no arguments are present (e.g. `$ myprog`).
20511926 ///
20521927 /// **NOTE:** [`subcommands`] count as arguments
@@ -3853,10 +3728,6 @@ impl Command {
38533728 self . max_w
38543729 }
38553730
3856- pub ( crate ) fn get_replacement ( & self , key : & str ) -> Option < & [ Str ] > {
3857- self . replacers . get ( key) . map ( |v| v. as_slice ( ) )
3858- }
3859-
38603731 pub ( crate ) fn get_keymap ( & self ) -> & MKeyMap {
38613732 & self . args
38623733 }
@@ -4749,7 +4620,6 @@ impl Default for Command {
47494620 g_settings : Default :: default ( ) ,
47504621 args : Default :: default ( ) ,
47514622 subcommands : Default :: default ( ) ,
4752- replacers : Default :: default ( ) ,
47534623 groups : Default :: default ( ) ,
47544624 current_help_heading : Default :: default ( ) ,
47554625 current_disp_ord : Some ( 0 ) ,
0 commit comments