|
5 | 5 |
|
6 | 6 | use clap::{crate_version, Arg, ArgAction, Command};
|
7 | 7 | use maps_format_parser::{parse_map_line, MapLine};
|
8 |
| -use pmap_config::{pmap_field_name, PmapConfig}; |
| 8 | +use pmap_config::{create_rc, pmap_field_name, PmapConfig}; |
9 | 9 | use smaps_format_parser::{parse_smaps, SmapTable};
|
10 | 10 | use std::env;
|
11 | 11 | use std::fs;
|
@@ -39,12 +39,61 @@ mod options {
|
39 | 39 | pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
40 | 40 | let matches = uu_app().try_get_matches_from(args)?;
|
41 | 41 |
|
| 42 | + if matches.get_flag(options::CREATE_RC) { |
| 43 | + let path = pmap_config::get_rc_default_path(); |
| 44 | + if std::fs::exists(&path)? { |
| 45 | + eprintln!("pmap: the file already exists - delete or rename it first"); |
| 46 | + eprintln!( |
| 47 | + "pmap: couldn't create {}", |
| 48 | + pmap_config::get_rc_default_path_str() |
| 49 | + ); |
| 50 | + set_exit_code(1); |
| 51 | + } else { |
| 52 | + create_rc(&path)?; |
| 53 | + eprintln!( |
| 54 | + "pmap: {} file successfully created, feel free to edit the content", |
| 55 | + pmap_config::get_rc_default_path_str() |
| 56 | + ); |
| 57 | + } |
| 58 | + return Ok(()); |
| 59 | + } else if let Some(path_str) = matches.get_one::<String>(options::CREATE_RC_TO) { |
| 60 | + let path = std::path::PathBuf::from(path_str); |
| 61 | + if std::fs::exists(&path)? { |
| 62 | + eprintln!("pmap: the file already exists - delete or rename it first"); |
| 63 | + eprintln!("pmap: couldn't create the rc file"); |
| 64 | + set_exit_code(1); |
| 65 | + } else { |
| 66 | + create_rc(&path)?; |
| 67 | + eprintln!("pmap: rc file successfully created, feel free to edit the content"); |
| 68 | + } |
| 69 | + return Ok(()); |
| 70 | + } |
| 71 | + |
42 | 72 | let mut pmap_config = PmapConfig::default();
|
43 | 73 |
|
44 | 74 | if matches.get_flag(options::MORE_EXTENDED) {
|
45 | 75 | pmap_config.set_more_extended();
|
46 | 76 | } else if matches.get_flag(options::MOST_EXTENDED) {
|
47 | 77 | pmap_config.set_most_extended();
|
| 78 | + } else if matches.get_flag(options::READ_RC) { |
| 79 | + let path = pmap_config::get_rc_default_path(); |
| 80 | + if !std::fs::exists(&path)? { |
| 81 | + eprintln!( |
| 82 | + "pmap: couldn't read {}", |
| 83 | + pmap_config::get_rc_default_path_str() |
| 84 | + ); |
| 85 | + set_exit_code(1); |
| 86 | + return Ok(()); |
| 87 | + } |
| 88 | + pmap_config.read_rc(&path)?; |
| 89 | + } else if let Some(path) = matches.get_one::<String>(options::READ_RC_FROM) { |
| 90 | + let path = std::fs::canonicalize(path)?; |
| 91 | + if !std::fs::exists(&path)? { |
| 92 | + eprintln!("pmap: couldn't read the rc file"); |
| 93 | + set_exit_code(1); |
| 94 | + return Ok(()); |
| 95 | + } |
| 96 | + pmap_config.read_rc(&path)?; |
48 | 97 | }
|
49 | 98 |
|
50 | 99 | // Options independent with field selection:
|
@@ -405,36 +454,81 @@ pub fn uu_app() -> Command {
|
405 | 454 | .short('c')
|
406 | 455 | .long("read-rc")
|
407 | 456 | .help("read the default rc")
|
408 |
| - .action(ArgAction::SetTrue), |
409 |
| - ) |
| 457 | + .action(ArgAction::SetTrue) |
| 458 | + .conflicts_with_all([ |
| 459 | + "read-rc-from", |
| 460 | + "device", |
| 461 | + "create-rc", |
| 462 | + "create-rc-to", |
| 463 | + "extended", |
| 464 | + "more-extended", |
| 465 | + "most-extended", |
| 466 | + ]), |
| 467 | + ) // pmap: options -c, -C, -d, -n, -N, -x, -X are mutually exclusive |
410 | 468 | .arg(
|
411 | 469 | Arg::new(options::READ_RC_FROM)
|
412 | 470 | .short('C')
|
413 | 471 | .long("read-rc-from")
|
414 | 472 | .num_args(1)
|
415 |
| - .help("read the rc from file"), |
416 |
| - ) |
| 473 | + .help("read the rc from file") |
| 474 | + .conflicts_with_all([ |
| 475 | + "read-rc", |
| 476 | + "device", |
| 477 | + "create-rc", |
| 478 | + "create-rc-to", |
| 479 | + "extended", |
| 480 | + "more-extended", |
| 481 | + "most-extended", |
| 482 | + ]), |
| 483 | + ) // pmap: options -c, -C, -d, -n, -N, -x, -X are mutually exclusive |
417 | 484 | .arg(
|
418 | 485 | Arg::new(options::CREATE_RC)
|
419 | 486 | .short('n')
|
420 | 487 | .long("create-rc")
|
421 | 488 | .help("create new default rc")
|
422 |
| - .action(ArgAction::SetTrue), |
423 |
| - ) |
| 489 | + .action(ArgAction::SetTrue) |
| 490 | + .conflicts_with_all([ |
| 491 | + "read-rc", |
| 492 | + "read-rc-from", |
| 493 | + "device", |
| 494 | + "create-rc-to", |
| 495 | + "extended", |
| 496 | + "more-extended", |
| 497 | + "most-extended", |
| 498 | + ]), |
| 499 | + ) // pmap: options -c, -C, -d, -n, -N, -x, -X are mutually exclusive |
424 | 500 | .arg(
|
425 | 501 | Arg::new(options::CREATE_RC_TO)
|
426 | 502 | .short('N')
|
427 | 503 | .long("create-rc-to")
|
428 | 504 | .num_args(1)
|
429 |
| - .help("create new rc to file"), |
430 |
| - ) |
| 505 | + .help("create new rc to file") |
| 506 | + .conflicts_with_all([ |
| 507 | + "read-rc", |
| 508 | + "read-rc-from", |
| 509 | + "device", |
| 510 | + "create-rc", |
| 511 | + "extended", |
| 512 | + "more-extended", |
| 513 | + "most-extended", |
| 514 | + ]), |
| 515 | + ) // pmap: options -c, -C, -d, -n, -N, -x, -X are mutually exclusive |
431 | 516 | .arg(
|
432 | 517 | Arg::new(options::DEVICE)
|
433 | 518 | .short('d')
|
434 | 519 | .long("device")
|
435 | 520 | .help("show the device format")
|
436 |
| - .action(ArgAction::SetTrue), |
437 |
| - ) |
| 521 | + .action(ArgAction::SetTrue) |
| 522 | + .conflicts_with_all([ |
| 523 | + "read-rc", |
| 524 | + "read-rc-from", |
| 525 | + "create-rc", |
| 526 | + "create-rc-to", |
| 527 | + "extended", |
| 528 | + "more-extended", |
| 529 | + "most-extended", |
| 530 | + ]), |
| 531 | + ) // pmap: options -c, -C, -d, -n, -N, -x, -X are mutually exclusive |
438 | 532 | .arg(
|
439 | 533 | Arg::new(options::QUIET)
|
440 | 534 | .short('q')
|
|
0 commit comments