Skip to content

Commit ad80304

Browse files
committed
Migrate examples from docopt to structopt.
1 parent 30f0138 commit ad80304

File tree

14 files changed

+236
-263
lines changed

14 files changed

+236
-263
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ openssl-sys = { version = "0.9.0", optional = true }
2727
openssl-probe = { version = "0.1", optional = true }
2828

2929
[dev-dependencies]
30-
docopt = "1.0"
31-
serde = "1.0"
32-
serde_derive = "1.0"
30+
structopt = "0.3"
3331
time = "0.1.39"
3432
tempfile = "3.1.0"
3533
thread-id = "3.3.0" # remove when we work with minimal-versions without it

examples/add.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
#![deny(warnings)]
1616
#![allow(trivial_casts)]
1717

18-
use docopt::Docopt;
1918
use git2::Repository;
20-
use serde_derive::Deserialize;
2119
use std::path::Path;
20+
use structopt::StructOpt;
2221

23-
#[derive(Deserialize)]
22+
#[derive(StructOpt)]
2423
struct Args {
24+
#[structopt(name = "spec")]
2525
arg_spec: Vec<String>,
26+
#[structopt(name = "dry_run", short = "n", long)]
27+
/// dry run
2628
flag_dry_run: bool,
29+
#[structopt(name = "verbose", short, long)]
30+
/// be verbose
2731
flag_verbose: bool,
32+
#[structopt(name = "update", short, long)]
33+
/// update tracked files
2834
flag_update: bool,
2935
}
3036

@@ -67,19 +73,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
6773
}
6874

6975
fn main() {
70-
const USAGE: &str = "
71-
usage: add [options] [--] [<spec>..]
72-
73-
Options:
74-
-n, --dry-run dry run
75-
-v, --verbose be verbose
76-
-u, --update update tracked files
77-
-h, --help show this message
78-
";
79-
80-
let args = Docopt::new(USAGE)
81-
.and_then(|d| d.deserialize())
82-
.unwrap_or_else(|e| e.exit());
76+
let args = Args::from_args();
8377
match run(&args) {
8478
Ok(()) => {}
8579
Err(e) => println!("error: {}", e),

examples/blame.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@
1212
* <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
*/
1414

15-
use docopt::Docopt;
15+
#![deny(warnings)]
16+
1617
use git2::{BlameOptions, Repository};
17-
use serde_derive::Deserialize;
1818
use std::io::{BufRead, BufReader};
1919
use std::path::Path;
20+
use structopt::StructOpt;
2021

21-
#[derive(Deserialize)]
22+
#[derive(StructOpt)]
2223
#[allow(non_snake_case)]
2324
struct Args {
25+
#[structopt(name = "path")]
2426
arg_path: String,
27+
#[structopt(name = "spec")]
2528
arg_spec: Option<String>,
29+
#[structopt(short = "M")]
30+
/// find line moves within and across files
2631
flag_M: bool,
32+
#[structopt(short = "C")]
33+
/// find line copies within and across files
2734
flag_C: bool,
35+
#[structopt(short = "F")]
36+
/// follow only the first parent commits
2837
flag_F: bool,
2938
}
3039

@@ -87,18 +96,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
8796
}
8897

8998
fn main() {
90-
const USAGE: &str = "
91-
usage: blame [options] [<spec>] <path>
92-
93-
Options:
94-
-M find line moves within and across files
95-
-C find line copies within and across files
96-
-F follow only the first parent commits
97-
";
98-
99-
let args = Docopt::new(USAGE)
100-
.and_then(|d| d.deserialize())
101-
.unwrap_or_else(|e| e.exit());
99+
let args = Args::from_args();
102100
match run(&args) {
103101
Ok(()) => {}
104102
Err(e) => println!("error: {}", e),

examples/cat-file.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,32 @@
1616

1717
use std::io::{self, Write};
1818

19-
use docopt::Docopt;
2019
use git2::{Blob, Commit, ObjectType, Repository, Signature, Tag, Tree};
21-
use serde_derive::Deserialize;
20+
use structopt::StructOpt;
2221

23-
#[derive(Deserialize)]
22+
#[derive(StructOpt)]
2423
struct Args {
24+
#[structopt(name = "object")]
2525
arg_object: String,
26+
#[structopt(short = "t")]
27+
/// show the object type
2628
flag_t: bool,
29+
#[structopt(short = "s")]
30+
/// show the object size
2731
flag_s: bool,
32+
#[structopt(short = "e")]
33+
/// suppress all output
2834
flag_e: bool,
35+
#[structopt(short = "p")]
36+
/// pretty print the contents of the object
2937
flag_p: bool,
38+
#[structopt(name = "quiet", short, long)]
39+
/// suppress output
3040
flag_q: bool,
41+
#[structopt(name = "verbose", short, long)]
3142
flag_v: bool,
43+
#[structopt(name = "dir", long = "git-dir")]
44+
/// use the specified directory as the base directory
3245
flag_git_dir: Option<String>,
3346
}
3447

@@ -128,23 +141,7 @@ fn show_sig(header: &str, sig: Option<Signature>) {
128141
}
129142

130143
fn main() {
131-
const USAGE: &str = "
132-
usage: cat-file (-t | -s | -e | -p) [options] <object>
133-
134-
Options:
135-
-t show the object type
136-
-s show the object size
137-
-e suppress all output
138-
-p pretty print the contents of the object
139-
-q suppress output
140-
-v use verbose output
141-
--git-dir <dir> use the specified directory as the base directory
142-
-h, --help show this message
143-
";
144-
145-
let args = Docopt::new(USAGE)
146-
.and_then(|d| d.deserialize())
147-
.unwrap_or_else(|e| e.exit());
144+
let args = Args::from_args();
148145
match run(&args) {
149146
Ok(()) => {}
150147
Err(e) => println!("error: {}", e),

examples/clone.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414

1515
#![deny(warnings)]
1616

17-
use docopt::Docopt;
1817
use git2::build::{CheckoutBuilder, RepoBuilder};
1918
use git2::{FetchOptions, Progress, RemoteCallbacks};
20-
use serde_derive::Deserialize;
2119
use std::cell::RefCell;
2220
use std::io::{self, Write};
2321
use std::path::{Path, PathBuf};
22+
use structopt::StructOpt;
2423

25-
#[derive(Deserialize)]
24+
#[derive(StructOpt)]
2625
struct Args {
26+
#[structopt(name = "url")]
2727
arg_url: String,
28+
#[structopt(name = "path")]
2829
arg_path: String,
2930
}
3031

@@ -117,16 +118,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
117118
}
118119

119120
fn main() {
120-
const USAGE: &str = "
121-
usage: add [options] <url> <path>
122-
123-
Options:
124-
-h, --help show this message
125-
";
126-
127-
let args = Docopt::new(USAGE)
128-
.and_then(|d| d.deserialize())
129-
.unwrap_or_else(|e| e.exit());
121+
let args = Args::from_args();
130122
match run(&args) {
131123
Ok(()) => {}
132124
Err(e) => println!("error: {}", e),

examples/diff.rs

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,113 @@
1414

1515
#![deny(warnings)]
1616

17-
use docopt::Docopt;
1817
use git2::{Diff, DiffOptions, Error, Object, ObjectType, Repository};
1918
use git2::{DiffFindOptions, DiffFormat};
20-
use serde_derive::Deserialize;
2119
use std::str;
20+
use structopt::StructOpt;
2221

23-
#[derive(Deserialize)]
22+
#[derive(StructOpt)]
2423
#[allow(non_snake_case)]
2524
struct Args {
25+
#[structopt(name = "from_oid")]
2626
arg_from_oid: Option<String>,
27+
#[structopt(name = "to_oid")]
2728
arg_to_oid: Option<String>,
29+
#[structopt(name = "patch", short, long)]
30+
/// show output in patch format
2831
flag_patch: bool,
32+
#[structopt(name = "cached", long)]
33+
/// use staged changes as diff
2934
flag_cached: bool,
35+
#[structopt(name = "nocached", long)]
36+
/// do not use staged changes
3037
flag_nocached: bool,
38+
#[structopt(name = "name-only", long)]
39+
/// show only names of changed files
3140
flag_name_only: bool,
41+
#[structopt(name = "name-status", long)]
42+
/// show only names and status changes
3243
flag_name_status: bool,
44+
#[structopt(name = "raw", long)]
45+
/// generate the raw format
3346
flag_raw: bool,
47+
#[structopt(name = "format", long)]
48+
/// specify format for stat summary
3449
flag_format: Option<String>,
50+
#[structopt(name = "color", long)]
51+
/// use color output
3552
flag_color: bool,
53+
#[structopt(name = "no-color", long)]
54+
/// never use color output
3655
flag_no_color: bool,
56+
#[structopt(short = "R")]
57+
/// swap two inputs
3758
flag_R: bool,
59+
#[structopt(name = "text", short = "a", long)]
60+
/// treat all files as text
3861
flag_text: bool,
62+
#[structopt(name = "ignore-space-at-eol", long)]
63+
/// ignore changes in whitespace at EOL
3964
flag_ignore_space_at_eol: bool,
65+
#[structopt(name = "ignore-space-change", short = "b", long)]
66+
/// ignore changes in amount of whitespace
4067
flag_ignore_space_change: bool,
68+
#[structopt(name = "ignore-all-space", short = "w", long)]
69+
/// ignore whitespace when comparing lines
4170
flag_ignore_all_space: bool,
71+
#[structopt(name = "ignored", long)]
72+
/// show untracked files
4273
flag_ignored: bool,
74+
#[structopt(name = "untracked", long)]
75+
/// generate diff using the patience algorithm
4376
flag_untracked: bool,
77+
#[structopt(name = "patience", long)]
78+
/// show ignored files as well
4479
flag_patience: bool,
80+
#[structopt(name = "minimal", long)]
81+
/// spend extra time to find smallest diff
4582
flag_minimal: bool,
83+
#[structopt(name = "stat", long)]
84+
/// generate a diffstat
4685
flag_stat: bool,
86+
#[structopt(name = "numstat", long)]
87+
/// similar to --stat, but more machine friendly
4788
flag_numstat: bool,
89+
#[structopt(name = "shortstat", long)]
90+
/// only output last line of --stat
4891
flag_shortstat: bool,
92+
#[structopt(name = "summary", long)]
93+
/// output condensed summary of header info
4994
flag_summary: bool,
95+
#[structopt(name = "find-renames", short = "M", long)]
96+
/// set threshold for findind renames (default 50)
5097
flag_find_renames: Option<u16>,
98+
#[structopt(name = "find-copies", short = "C", long)]
99+
/// set threshold for finding copies (default 50)
51100
flag_find_copies: Option<u16>,
101+
#[structopt(name = "find-copies-harder", long)]
102+
/// inspect unmodified files for sources of copies
52103
flag_find_copies_harder: bool,
104+
#[structopt(name = "break_rewrites", short = "B", long)]
105+
/// break complete rewrite changes into pairs
53106
flag_break_rewrites: bool,
107+
#[structopt(name = "unified", short = "U", long)]
108+
/// lints of context to show
54109
flag_unified: Option<u32>,
110+
#[structopt(name = "inter-hunk-context", long)]
111+
/// maximum lines of change between hunks
55112
flag_inter_hunk_context: Option<u32>,
113+
#[structopt(name = "abbrev", long)]
114+
/// length to abbreviate commits to
56115
flag_abbrev: Option<u16>,
116+
#[structopt(name = "src-prefix", long)]
117+
/// show given source prefix instead of 'a/'
57118
flag_src_prefix: Option<String>,
119+
#[structopt(name = "dst-prefix", long)]
120+
/// show given destinction prefix instead of 'b/'
58121
flag_dst_prefix: Option<String>,
122+
#[structopt(name = "path", long = "git-dir")]
123+
/// path to git repository to use
59124
flag_git_dir: Option<String>,
60125
}
61126

@@ -262,48 +327,7 @@ impl Args {
262327
}
263328

264329
fn main() {
265-
const USAGE: &str = "
266-
usage: diff [options] [<from-oid> [<to-oid>]]
267-
268-
Options:
269-
-p, --patch show output in patch format
270-
--cached use staged changes as diff
271-
--nocached do not use staged changes
272-
--name-only show only names of changed files
273-
--name-status show only names and status changes
274-
--raw generate the raw format
275-
--format=<format> specify format for stat summary
276-
--color use color output
277-
--no-color never use color output
278-
-R swap two inputs
279-
-a, --text treat all files as text
280-
--ignore-space-at-eol ignore changes in whitespace at EOL
281-
-b, --ignore-space-change ignore changes in amount of whitespace
282-
-w, --ignore-all-space ignore whitespace when comparing lines
283-
--ignored show ignored files as well
284-
--untracked show untracked files
285-
--patience generate diff using the patience algorithm
286-
--minimal spend extra time to find smallest diff
287-
--stat generate a diffstat
288-
--numstat similar to --stat, but more machine friendly
289-
--shortstat only output last line of --stat
290-
--summary output condensed summary of header info
291-
-M, --find-renames <n> set threshold for findind renames (default 50)
292-
-C, --find-copies <n> set threshold for finding copies (default 50)
293-
--find-copies-harder inspect unmodified files for sources of copies
294-
-B, --break-rewrites break complete rewrite changes into pairs
295-
-U, --unified <n> lints of context to show
296-
--inter-hunk-context <n> maximum lines of change between hunks
297-
--abbrev <n> length to abbreviate commits to
298-
--src-prefix <s> show given source prefix instead of 'a/'
299-
--dst-prefix <s> show given destinction prefix instead of 'b/'
300-
--git-dir <path> path to git repository to use
301-
-h, --help show this message
302-
";
303-
304-
let args = Docopt::new(USAGE)
305-
.and_then(|d| d.deserialize())
306-
.unwrap_or_else(|e| e.exit());
330+
let args = Args::from_args();
307331
match run(&args) {
308332
Ok(()) => {}
309333
Err(e) => println!("error: {}", e),

0 commit comments

Comments
 (0)