##// END OF EJS Templates
rhg: Add parsing for the --color global CLI argument...
Simon Sapin -
r49583:d4a5c219 default
parent child Browse files
Show More
@@ -139,13 +139,19 b' impl Config {'
139 Ok(config)
139 Ok(config)
140 }
140 }
141
141
142 pub fn load_cli_args_config(
142 pub fn load_cli_args(
143 &mut self,
143 &mut self,
144 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
144 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
145 color_arg: Option<Vec<u8>>,
145 ) -> Result<(), ConfigError> {
146 ) -> Result<(), ConfigError> {
146 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
147 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
147 self.layers.push(layer)
148 self.layers.push(layer)
148 }
149 }
150 if let Some(arg) = color_arg {
151 let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor);
152 layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None);
153 self.layers.push(layer)
154 }
149 Ok(())
155 Ok(())
150 }
156 }
151
157
@@ -301,10 +301,11 b' pub enum ConfigOrigin {'
301 File(PathBuf),
301 File(PathBuf),
302 /// From a `--config` CLI argument
302 /// From a `--config` CLI argument
303 CommandLine,
303 CommandLine,
304 /// From a `--color` CLI argument
305 CommandLineColor,
304 /// From environment variables like `$PAGER` or `$EDITOR`
306 /// From environment variables like `$PAGER` or `$EDITOR`
305 Environment(Vec<u8>),
307 Environment(Vec<u8>),
306 /* TODO cli
308 /* TODO defaults (configitems.py)
307 * TODO defaults (configitems.py)
308 * TODO extensions
309 * TODO extensions
309 * TODO Python resources?
310 * TODO Python resources?
310 * Others? */
311 * Others? */
@@ -318,6 +319,7 b' impl DisplayBytes for ConfigOrigin {'
318 match self {
319 match self {
319 ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)),
320 ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)),
320 ConfigOrigin::CommandLine => out.write_all(b"--config"),
321 ConfigOrigin::CommandLine => out.write_all(b"--config"),
322 ConfigOrigin::CommandLineColor => out.write_all(b"--color"),
321 ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e),
323 ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e),
322 }
324 }
323 }
325 }
@@ -66,6 +66,14 b' fn main_with_result('
66 .takes_value(true)
66 .takes_value(true)
67 .global(true),
67 .global(true),
68 )
68 )
69 .arg(
70 Arg::with_name("color")
71 .help("when to colorize (boolean, always, auto, never, or debug)")
72 .long("--color")
73 .value_name("TYPE")
74 .takes_value(true)
75 .global(true),
76 )
69 .version("0.0.1");
77 .version("0.0.1");
70 let app = add_subcommand_args(app);
78 let app = add_subcommand_args(app);
71
79
@@ -179,7 +187,7 b' fn main() {'
179 });
187 });
180
188
181 non_repo_config
189 non_repo_config
182 .load_cli_args_config(early_args.config)
190 .load_cli_args(early_args.config, early_args.color)
183 .unwrap_or_else(|error| {
191 .unwrap_or_else(|error| {
184 exit(
192 exit(
185 &initial_current_dir,
193 &initial_current_dir,
@@ -526,6 +534,8 b' struct NoRepoInCwdError {'
526 struct EarlyArgs {
534 struct EarlyArgs {
527 /// Values of all `--config` arguments. (Possibly none)
535 /// Values of all `--config` arguments. (Possibly none)
528 config: Vec<Vec<u8>>,
536 config: Vec<Vec<u8>>,
537 /// Value of all the `--color` argument, if any.
538 color: Option<Vec<u8>>,
529 /// Value of the `-R` or `--repository` argument, if any.
539 /// Value of the `-R` or `--repository` argument, if any.
530 repo: Option<Vec<u8>>,
540 repo: Option<Vec<u8>>,
531 /// Value of the `--cwd` argument, if any.
541 /// Value of the `--cwd` argument, if any.
@@ -536,6 +546,7 b' impl EarlyArgs {'
536 fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
546 fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
537 let mut args = args.into_iter().map(get_bytes_from_os_str);
547 let mut args = args.into_iter().map(get_bytes_from_os_str);
538 let mut config = Vec::new();
548 let mut config = Vec::new();
549 let mut color = None;
539 let mut repo = None;
550 let mut repo = None;
540 let mut cwd = None;
551 let mut cwd = None;
541 // Use `while let` instead of `for` so that we can also call
552 // Use `while let` instead of `for` so that we can also call
@@ -549,6 +560,14 b' impl EarlyArgs {'
549 config.push(value.to_owned())
560 config.push(value.to_owned())
550 }
561 }
551
562
563 if arg == b"--color" {
564 if let Some(value) = args.next() {
565 color = Some(value)
566 }
567 } else if let Some(value) = arg.drop_prefix(b"--color=") {
568 color = Some(value.to_owned())
569 }
570
552 if arg == b"--cwd" {
571 if arg == b"--cwd" {
553 if let Some(value) = args.next() {
572 if let Some(value) = args.next() {
554 cwd = Some(value)
573 cwd = Some(value)
@@ -567,7 +586,12 b' impl EarlyArgs {'
567 repo = Some(value.to_owned())
586 repo = Some(value.to_owned())
568 }
587 }
569 }
588 }
570 Self { config, repo, cwd }
589 Self {
590 config,
591 color,
592 repo,
593 cwd,
594 }
571 }
595 }
572 }
596 }
573
597
General Comments 0
You need to be logged in to leave comments. Login now