Skip to content

Commit 6acae17

Browse files
committed
uu_awk: start using clap
1 parent 9ce8327 commit 6acae17

4 files changed

Lines changed: 97 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ bumpalo.workspace = true
4444
rustix = { version = "1.1.4", default-features = false, features = ["process"]}
4545
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
4646
tracing-error = "0.2.1"
47+
clap = { version = "4.6.1", features = ["derive"] }
4748

4849
[dev-dependencies]
4950
ctor = "0.4"
@@ -61,9 +62,6 @@ inherits = "release"
6162
opt-level = "z"
6263
strip = true
6364

64-
[profile.dev.package.backtrace]
65-
opt-level = 3
66-
6765
# A release-like profile with debug info for profiling.
6866
# See https://github.com/mstange/samply .
6967
[profile.profiling]

src/cli.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// This file is part of the uutils awk package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// files that was distributed with this source code.
5+
6+
use std::{ffi::OsString, path::PathBuf};
7+
8+
use clap::Parser;
9+
10+
#[derive(Parser, Debug)]
11+
#[clap(version, name = "uutils AWK")]
12+
#[clap(about = ::std::concat!("uutils awk ", ::std::env!("CARGO_PKG_VERSION")))]
13+
pub struct Args {
14+
// POSIX
15+
pub code: OsString,
16+
#[arg(short = 'f', long)]
17+
file: Option<PathBuf>,
18+
#[arg(short = 'F', long)]
19+
field_separator: Option<OsString>,
20+
#[arg(short = 'v', long, value_parser = parse_kv)]
21+
assign: Vec<(String, String)>,
22+
#[arg(short = 'b', long)]
23+
characters_as_bytes: bool,
24+
#[arg(short = 'c', long)]
25+
traditional: bool,
26+
#[arg(short = 'C', long)]
27+
copyright: bool,
28+
#[arg(short = 'd', long)]
29+
dump_variables: Option<PathBuf>,
30+
#[arg(short = 'D', long)]
31+
debug: Option<PathBuf>,
32+
#[arg(short = 'e', long)]
33+
source: Vec<u8>,
34+
#[arg(short = 'E', long)]
35+
exec: Option<PathBuf>,
36+
#[arg(short = 'g', long)]
37+
gen_pot: bool,
38+
#[arg(short = 'i', long)]
39+
include: Option<PathBuf>,
40+
#[arg(short = 'I', long)]
41+
trace: bool,
42+
#[arg(short = 'l', long)]
43+
load: Vec<OsString>,
44+
#[arg(short = 'L', long)]
45+
lint: Vec<String>,
46+
#[arg(short = 'M', long)]
47+
bignum: bool,
48+
#[arg(short = 'n', long)]
49+
non_decimal_data: bool,
50+
#[arg(short = 'N', long)]
51+
use_lc_numeric: bool,
52+
#[arg(short = 'o', long)]
53+
pretty_print: Option<PathBuf>,
54+
#[arg(short = 'O', long, default_value_t = true)]
55+
optimize: bool,
56+
#[arg(short = 's', long = "no-optimize")]
57+
no_optimize: bool,
58+
#[arg(short = 'p', long)]
59+
profile: Option<PathBuf>,
60+
#[arg(short = 'P', long)]
61+
posix: bool,
62+
#[arg(short = 'r', long, default_value_t = true)]
63+
re_interval: bool,
64+
#[arg(short = 'S', long)]
65+
sandbox: bool,
66+
#[arg(short = 't', long)]
67+
lint_old: bool,
68+
}
69+
70+
fn parse_kv(s: &str) -> Result<(String, String), String> {
71+
let (k, v) = s.split_once('=').ok_or("expected key=value")?;
72+
Ok((k.to_string(), v.trim_matches(['"', '\'']).to_string()))
73+
}

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55

66
// static POSIX: bool = false;
77

8+
mod cli;
89
mod utils;
910

1011
use bumpalo::Bump;
12+
use clap::Parser as _;
1113
use color_eyre::Result;
1214
use parser::Parser;
1315

14-
use crate::utils::{ensure_consistent_panic, exit_err};
16+
use crate::{
17+
cli::Args,
18+
utils::{ensure_consistent_panic, exit_err},
19+
};
1520

1621
fn main() {
1722
if let Err(e) = ensure_consistent_panic(uu_main) {
@@ -21,11 +26,12 @@ fn main() {
2126

2227
#[tracing::instrument]
2328
fn uu_main() -> Result<()> {
24-
let arg = std::env::args().nth(1).unwrap();
29+
let args = Args::parse();
30+
println!("{args:?}");
2531

2632
let arena = Bump::with_capacity(4000); // 4KB minus metadata-ish
2733
let mut parser = Parser::new(&arena);
28-
let ast = match parser.parse("CLI", arg.as_bytes()) {
34+
let ast = match parser.parse("CLI", args.code.as_encoded_bytes()) {
2935
Ok(ast) => dbg!(ast),
3036
Err((report, source)) => {
3137
report.eprint(("CLI", source)).unwrap();

0 commit comments

Comments
 (0)