Skip to content

Commit bdc8639

Browse files
committed
Add option to generate completion script.
1 parent 7a49423 commit bdc8639

13 files changed

+101
-32
lines changed

Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ license = "MIT"
1313
jbk = { git = "https://github.com/jubako/jubako.git", package = "jubako", version = "0.2.0" }
1414
clap = { version = "4.4.5", features = ["derive"] }
1515
clap_mangen = "0.2.20"
16+
clap_complete = "4.5.0"
1617
indicatif = "0.17.7"
1718

1819
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

arx/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ arx = { path = "../libarx", version = "0.2.0", package="libarx", features=["cmd_
1515
jbk.workspace = true
1616
clap.workspace = true
1717
clap_mangen.workspace = true
18+
clap_complete.workspace = true
1819
indicatif.workspace = true
1920
env_logger = "0.10.0"
2021
anyhow = "1.0.75"

arx/src/create.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use std::path::PathBuf;
77
use std::rc::Rc;
88
use std::sync::Arc;
99

10+
use clap::{Parser, ValueHint};
11+
1012
/// Create an archive.
11-
#[derive(clap::Parser, Debug)]
13+
#[derive(Parser, Debug)]
1214
pub struct Options {
1315
/// File path of the archive to create.
1416
///
@@ -18,18 +20,19 @@ pub struct Options {
1820
short = 'f',
1921
long = "file",
2022
value_parser,
21-
required_unless_present("list_compressions")
23+
required_unless_present("list_compressions"),
24+
value_hint=ValueHint::FilePath
2225
)]
2326
outfile: Option<PathBuf>,
2427

2528
/// Remove STRIP_PREFIX from the entries' name added to the archive.
26-
#[arg(long, required = false)]
29+
#[arg(long, required = false, value_hint=ValueHint::DirPath)]
2730
strip_prefix: Option<arx::PathBuf>,
2831

2932
/// Move to BASE_DIR before starting adding content to arx archive.
3033
///
3134
/// Argument `INFILES` or `STRIP_PREFIX` must be relative to `BASE_DIR`.
32-
#[arg(short = 'C', required = false)]
35+
#[arg(short = 'C', required = false, value_hint=ValueHint::DirPath)]
3336
base_dir: Option<PathBuf>,
3437

3538
/// Input files/directories
@@ -38,7 +41,7 @@ pub struct Options {
3841
///
3942
/// In this mode `recurse` is true by default.
4043
/// Use `--no-recurse` to avoid recursion.
41-
#[arg(value_parser, group = "input")]
44+
#[arg(value_parser, group = "input", value_hint=ValueHint::AnyPath)]
4245
infiles: Vec<PathBuf>,
4346

4447
/// Get the list of files/directories to add from the FILE_LIST (incompatible with INFILES)
@@ -48,7 +51,7 @@ pub struct Options {
4851
/// In this mode, `recurse` is false by default.
4952
/// This allow FILE_LIST listing both the directory and (subset of) files in the given directory.
5053
/// Use `--recurse` to activate recursion.
51-
#[arg(short = 'L', long = "file-list", group = "input", verbatim_doc_comment)]
54+
#[arg(short = 'L', long = "file-list", group = "input", verbatim_doc_comment, value_hint=ValueHint::FilePath)]
5255
file_list: Option<PathBuf>,
5356

5457
/// Recurse in directories

arx/src/dump.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clap::{Parser, ValueHint};
12
use jbk::reader::builder::PropertyBuilderTrait;
23
use log::info;
34
use std::path::PathBuf;
@@ -39,18 +40,18 @@ fn dump_entry(
3940
}
4041

4142
/// Print the content of an entry in the archive.
42-
#[derive(clap::Parser, Debug)]
43+
#[derive(Parser, Debug)]
4344
pub struct Options {
4445
/// Archive to read
45-
#[arg(value_parser)]
46+
#[arg(value_parser, value_hint=ValueHint::FilePath)]
4647
infile: PathBuf,
4748

4849
/// Path of the entry to print
4950
#[arg(value_parser)]
5051
path: arx::PathBuf,
5152

5253
/// Output Path. If not present or -, print to stdout
53-
#[arg(value_parser)]
54+
#[arg(value_parser, value_hint=ValueHint::FilePath)]
5455
output: Option<String>,
5556

5657
#[arg(from_global)]

arx/src/extract.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clap::{Parser, ValueHint};
12
use log::info;
23
use std::collections::HashSet;
34
use std::env::current_dir;
@@ -6,26 +7,31 @@ use std::io::{BufRead, BufReader};
67
use std::path::PathBuf;
78

89
/// Extract the content of an archive
9-
#[derive(clap::Parser, Debug)]
10+
#[derive(Parser, Debug)]
1011
pub struct Options {
1112
/// Archive to read
12-
#[arg(short = 'f', long = "file")]
13+
#[arg(short = 'f', long = "file", value_hint=ValueHint::FilePath)]
1314
infile: PathBuf,
1415

1516
/// Directory in which extract the archive. (Default to current directory)
16-
#[arg(short = 'C', required = false)]
17+
#[arg(short = 'C', required = false, value_hint=ValueHint::DirPath)]
1718
outdir: Option<PathBuf>,
1819

1920
/// Files to extract
20-
#[arg(value_parser, group = "input")]
21+
#[arg(value_parser, group = "input", value_hint=ValueHint::AnyPath)]
2122
extract_files: Vec<arx::PathBuf>,
2223

2324
/// Print a progress bar of the extraction
2425
#[arg(short = 'p', long = "progress", default_value_t = false, action)]
2526
progress: bool,
2627

2728
/// Get the list of files/directories to extract from the FILE_LIST (incompatible with EXTRACT_FILES)
28-
#[arg(short = 'L', long = "file-list", group = "input")]
29+
#[arg(
30+
short = 'L',
31+
long = "file-list",
32+
group = "input",
33+
value_hint = ValueHint::FilePath
34+
)]
2935
file_list: Option<PathBuf>,
3036

3137
#[arg(from_global)]

arx/src/list.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::ops::DerefMut;
88
use std::path::PathBuf;
99

1010
use anyhow::{anyhow, Context, Result};
11+
use clap::{Parser, ValueHint};
1112

1213
type Path = Vec<u8>;
1314

@@ -135,10 +136,10 @@ where
135136
}
136137

137138
/// List the content in an archive.
138-
#[derive(clap::Parser, Debug)]
139+
#[derive(Parser, Debug)]
139140
pub struct Options {
140141
/// Archive to read
141-
#[arg(value_parser)]
142+
#[arg(value_parser, value_hint= ValueHint::FilePath)]
142143
infile: PathBuf,
143144

144145
/// Use stable output (for scripting)

arx/src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ struct Cli {
3434
)]
3535
generate_man_page: Option<String>,
3636

37+
#[arg(long, help_heading = "Advanced")]
38+
generate_complete: Option<clap_complete::Shell>,
39+
3740
#[command(subcommand)]
3841
command: Option<Commands>,
3942
}
@@ -96,6 +99,13 @@ fn run() -> Result<()> {
9699
return Ok(());
97100
}
98101

102+
if let Some(what) = args.generate_complete {
103+
let mut command = Cli::command();
104+
let name = command.get_name().to_string();
105+
clap_complete::generate(what, &mut command, name, &mut std::io::stdout());
106+
return Ok(());
107+
}
108+
99109
match args.command {
100110
None => Ok(Cli::command().print_help()?),
101111
Some(c) => match c {

arx/src/mount.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clap::{Parser, ValueHint};
12
use log::info;
23
use std::path::PathBuf;
34

@@ -89,14 +90,14 @@ impl std::fmt::Display for StatCounter {
8990
}
9091

9192
/// Mount an archive in a directory.
92-
#[derive(clap::Parser, Debug)]
93+
#[derive(Parser, Debug)]
9394
pub struct Options {
9495
/// Archive to read
95-
#[arg(value_parser)]
96+
#[arg(value_parser, value_hint=ValueHint::FilePath)]
9697
infile: PathBuf,
9798

9899
/// Target directory
99-
#[arg(value_parser)]
100+
#[arg(value_parser, value_hint=ValueHint::DirPath)]
100101
mountdir: PathBuf,
101102

102103
#[arg(from_global)]

tar2arx/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ jbk.workspace = true
1616
clap.workspace = true
1717
indicatif.workspace = true
1818
clap_mangen.workspace = true
19+
clap_complete.workspace = true
1920
tar = "0.4.39"
2021
niffler = "2.5.0"

tar2arx/src/main.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{CommandFactory, Parser};
1+
use clap::{CommandFactory, Parser, ValueHint};
22

33
use arx::create::Adder;
44
use std::io::Read;
@@ -13,18 +13,18 @@ use std::sync::Arc;
1313
#[command(name = "tar2arx", author, version, about, long_about=None)]
1414
struct Cli {
1515
/// Tar file to convert
16-
#[arg(value_parser)]
17-
tar_file: Option<String>,
18-
19-
#[arg(long, help_heading = "Advanced")]
20-
generate_man_page: bool,
16+
#[arg(value_parser, value_hint=ValueHint::FilePath)]
17+
tar_file: Option<PathBuf>,
2118

2219
/// Archive name to create
2320
#[arg(
2421
short,
2522
long,
2623
value_parser,
27-
required_unless_present_any(["list_compressions", "generate_man_page"])
24+
required_unless_present_any(
25+
["list_compressions", "generate_man_page", "generate_complete"]
26+
),
27+
value_hint=ValueHint::FilePath
2828
)]
2929
outfile: Option<PathBuf>,
3030

@@ -44,6 +44,13 @@ struct Cli {
4444
/// List available compression algorithms
4545
#[arg(long, default_value_t = false, action)]
4646
list_compressions: bool,
47+
48+
#[arg(long, help_heading = "Advanced")]
49+
generate_man_page: bool,
50+
51+
#[arg(long, help_heading = "Advanced")]
52+
generate_complete: Option<clap_complete::Shell>,
53+
4754
}
4855

4956
#[derive(Clone)]
@@ -272,6 +279,13 @@ fn main() -> jbk::Result<()> {
272279
return Ok(());
273280
}
274281

282+
if let Some(what) = args.generate_complete {
283+
let mut command = Cli::command();
284+
let name = command.get_name().to_string();
285+
clap_complete::generate(what, &mut command, name, &mut std::io::stdout());
286+
return Ok(());
287+
}
288+
275289
let mut input_size = None;
276290
let input: Box<dyn Read> = match args.tar_file {
277291
None => Box::new(std::io::stdin()),

zip2arx/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ jbk.workspace = true
1616
clap.workspace = true
1717
indicatif.workspace = true
1818
clap_mangen.workspace = true
19+
clap_complete.workspace = true
1920
zip = "0.6.6"
2021
flate2 = "1.0.26"

zip2arx/src/main.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{CommandFactory, Parser};
1+
use clap::{CommandFactory, Parser, ValueHint};
22

33
use arx::create::Adder;
44
use std::io::{Read, Seek};
@@ -13,19 +13,22 @@ struct Cli {
1313
/// Zip file to convert
1414
#[arg(
1515
value_parser,
16-
required_unless_present_any(["list_compressions", "generate_man_page"])
16+
required_unless_present_any(
17+
["list_compressions", "generate_man_page", "generate_complete"]
18+
),
19+
value_hint=ValueHint::FilePath
1720
)]
1821
zip_file: Option<PathBuf>,
1922

20-
#[arg(long, help_heading = "Advanced")]
21-
generate_man_page: bool,
22-
2323
/// Archive name to create
2424
#[arg(
2525
short,
2626
long,
2727
value_parser,
28-
required_unless_present_any(["list_compressions", "generate_man_page"])
28+
required_unless_present_any(
29+
["list_compressions", "generate_man_page", "generate_complete"]
30+
),
31+
value_hint=ValueHint::FilePath
2932
)]
3033
outfile: Option<PathBuf>,
3134

@@ -45,6 +48,13 @@ struct Cli {
4548
/// List available compression algorithms
4649
#[arg(long, default_value_t = false, action)]
4750
list_compressions: bool,
51+
52+
#[arg(long, help_heading = "Advanced")]
53+
generate_man_page: bool,
54+
55+
#[arg(long, help_heading = "Advanced")]
56+
generate_complete: Option<clap_complete::Shell>,
57+
4858
}
4959

5060
#[derive(Clone)]
@@ -242,6 +252,13 @@ fn main() -> jbk::Result<()> {
242252
return Ok(());
243253
}
244254

255+
if let Some(what) = args.generate_complete {
256+
let mut command = Cli::command();
257+
let name = command.get_name().to_string();
258+
clap_complete::generate(what, &mut command, name, &mut std::io::stdout());
259+
return Ok(());
260+
}
261+
245262
let file = std::fs::File::open(args.zip_file.as_ref().unwrap())?;
246263
let archive = zip::ZipArchive::new(file).unwrap();
247264
let converter = Converter::new(

0 commit comments

Comments
 (0)