Skip to content

Migrate examples from docopt to structopt. #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ openssl-sys = { version = "0.9.0", optional = true }
openssl-probe = { version = "0.1", optional = true }

[dev-dependencies]
docopt = "1.0"
serde = "1.0"
serde_derive = "1.0"
structopt = "0.3"
time = "0.1.39"
tempfile = "3.1.0"
thread-id = "3.3.0" # remove when we work with minimal-versions without it
Expand Down
26 changes: 10 additions & 16 deletions examples/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@
#![deny(warnings)]
#![allow(trivial_casts)]

use docopt::Docopt;
use git2::Repository;
use serde_derive::Deserialize;
use std::path::Path;
use structopt::StructOpt;

#[derive(Deserialize)]
#[derive(StructOpt)]
struct Args {
#[structopt(name = "spec")]
arg_spec: Vec<String>,
#[structopt(name = "dry_run", short = "n", long)]
/// dry run
flag_dry_run: bool,
#[structopt(name = "verbose", short, long)]
/// be verbose
flag_verbose: bool,
#[structopt(name = "update", short, long)]
/// update tracked files
flag_update: bool,
}

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

fn main() {
const USAGE: &str = "
usage: add [options] [--] [<spec>..]

Options:
-n, --dry-run dry run
-v, --verbose be verbose
-u, --update update tracked files
-h, --help show this message
";

let args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let args = Args::from_args();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
Expand Down
28 changes: 13 additions & 15 deletions examples/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

use docopt::Docopt;
#![deny(warnings)]

use git2::{BlameOptions, Repository};
use serde_derive::Deserialize;
use std::io::{BufRead, BufReader};
use std::path::Path;
use structopt::StructOpt;

#[derive(Deserialize)]
#[derive(StructOpt)]
#[allow(non_snake_case)]
struct Args {
#[structopt(name = "path")]
arg_path: String,
#[structopt(name = "spec")]
arg_spec: Option<String>,
#[structopt(short = "M")]
/// find line moves within and across files
flag_M: bool,
#[structopt(short = "C")]
/// find line copies within and across files
flag_C: bool,
#[structopt(short = "F")]
/// follow only the first parent commits
flag_F: bool,
}

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

fn main() {
const USAGE: &str = "
usage: blame [options] [<spec>] <path>

Options:
-M find line moves within and across files
-C find line copies within and across files
-F follow only the first parent commits
";

let args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let args = Args::from_args();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
Expand Down
37 changes: 17 additions & 20 deletions examples/cat-file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@

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

use docopt::Docopt;
use git2::{Blob, Commit, ObjectType, Repository, Signature, Tag, Tree};
use serde_derive::Deserialize;
use structopt::StructOpt;

#[derive(Deserialize)]
#[derive(StructOpt)]
struct Args {
#[structopt(name = "object")]
arg_object: String,
#[structopt(short = "t")]
/// show the object type
flag_t: bool,
#[structopt(short = "s")]
/// show the object size
flag_s: bool,
#[structopt(short = "e")]
/// suppress all output
flag_e: bool,
#[structopt(short = "p")]
/// pretty print the contents of the object
flag_p: bool,
#[structopt(name = "quiet", short, long)]
/// suppress output
flag_q: bool,
#[structopt(name = "verbose", short, long)]
flag_v: bool,
#[structopt(name = "dir", long = "git-dir")]
/// use the specified directory as the base directory
flag_git_dir: Option<String>,
}

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

fn main() {
const USAGE: &str = "
usage: cat-file (-t | -s | -e | -p) [options] <object>

Options:
-t show the object type
-s show the object size
-e suppress all output
-p pretty print the contents of the object
-q suppress output
-v use verbose output
--git-dir <dir> use the specified directory as the base directory
-h, --help show this message
";

let args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let args = Args::from_args();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
Expand Down
18 changes: 5 additions & 13 deletions examples/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@

#![deny(warnings)]

use docopt::Docopt;
use git2::build::{CheckoutBuilder, RepoBuilder};
use git2::{FetchOptions, Progress, RemoteCallbacks};
use serde_derive::Deserialize;
use std::cell::RefCell;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use structopt::StructOpt;

#[derive(Deserialize)]
#[derive(StructOpt)]
struct Args {
#[structopt(name = "url")]
arg_url: String,
#[structopt(name = "path")]
arg_path: String,
}

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

fn main() {
const USAGE: &str = "
usage: add [options] <url> <path>

Options:
-h, --help show this message
";

let args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let args = Args::from_args();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
Expand Down
114 changes: 69 additions & 45 deletions examples/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,113 @@

#![deny(warnings)]

use docopt::Docopt;
use git2::{Diff, DiffOptions, Error, Object, ObjectType, Repository};
use git2::{DiffFindOptions, DiffFormat};
use serde_derive::Deserialize;
use std::str;
use structopt::StructOpt;

#[derive(Deserialize)]
#[derive(StructOpt)]
#[allow(non_snake_case)]
struct Args {
#[structopt(name = "from_oid")]
arg_from_oid: Option<String>,
#[structopt(name = "to_oid")]
arg_to_oid: Option<String>,
#[structopt(name = "patch", short, long)]
/// show output in patch format
flag_patch: bool,
#[structopt(name = "cached", long)]
/// use staged changes as diff
flag_cached: bool,
#[structopt(name = "nocached", long)]
/// do not use staged changes
flag_nocached: bool,
#[structopt(name = "name-only", long)]
/// show only names of changed files
flag_name_only: bool,
#[structopt(name = "name-status", long)]
/// show only names and status changes
flag_name_status: bool,
#[structopt(name = "raw", long)]
/// generate the raw format
flag_raw: bool,
#[structopt(name = "format", long)]
/// specify format for stat summary
flag_format: Option<String>,
#[structopt(name = "color", long)]
/// use color output
flag_color: bool,
#[structopt(name = "no-color", long)]
/// never use color output
flag_no_color: bool,
#[structopt(short = "R")]
/// swap two inputs
flag_R: bool,
#[structopt(name = "text", short = "a", long)]
/// treat all files as text
flag_text: bool,
#[structopt(name = "ignore-space-at-eol", long)]
/// ignore changes in whitespace at EOL
flag_ignore_space_at_eol: bool,
#[structopt(name = "ignore-space-change", short = "b", long)]
/// ignore changes in amount of whitespace
flag_ignore_space_change: bool,
#[structopt(name = "ignore-all-space", short = "w", long)]
/// ignore whitespace when comparing lines
flag_ignore_all_space: bool,
#[structopt(name = "ignored", long)]
/// show untracked files
flag_ignored: bool,
#[structopt(name = "untracked", long)]
/// generate diff using the patience algorithm
flag_untracked: bool,
#[structopt(name = "patience", long)]
/// show ignored files as well
flag_patience: bool,
#[structopt(name = "minimal", long)]
/// spend extra time to find smallest diff
flag_minimal: bool,
#[structopt(name = "stat", long)]
/// generate a diffstat
flag_stat: bool,
#[structopt(name = "numstat", long)]
/// similar to --stat, but more machine friendly
flag_numstat: bool,
#[structopt(name = "shortstat", long)]
/// only output last line of --stat
flag_shortstat: bool,
#[structopt(name = "summary", long)]
/// output condensed summary of header info
flag_summary: bool,
#[structopt(name = "find-renames", short = "M", long)]
/// set threshold for findind renames (default 50)
flag_find_renames: Option<u16>,
#[structopt(name = "find-copies", short = "C", long)]
/// set threshold for finding copies (default 50)
flag_find_copies: Option<u16>,
#[structopt(name = "find-copies-harder", long)]
/// inspect unmodified files for sources of copies
flag_find_copies_harder: bool,
#[structopt(name = "break_rewrites", short = "B", long)]
/// break complete rewrite changes into pairs
flag_break_rewrites: bool,
#[structopt(name = "unified", short = "U", long)]
/// lints of context to show
flag_unified: Option<u32>,
#[structopt(name = "inter-hunk-context", long)]
/// maximum lines of change between hunks
flag_inter_hunk_context: Option<u32>,
#[structopt(name = "abbrev", long)]
/// length to abbreviate commits to
flag_abbrev: Option<u16>,
#[structopt(name = "src-prefix", long)]
/// show given source prefix instead of 'a/'
flag_src_prefix: Option<String>,
#[structopt(name = "dst-prefix", long)]
/// show given destinction prefix instead of 'b/'
flag_dst_prefix: Option<String>,
#[structopt(name = "path", long = "git-dir")]
/// path to git repository to use
flag_git_dir: Option<String>,
}

Expand Down Expand Up @@ -262,48 +327,7 @@ impl Args {
}

fn main() {
const USAGE: &str = "
usage: diff [options] [<from-oid> [<to-oid>]]

Options:
-p, --patch show output in patch format
--cached use staged changes as diff
--nocached do not use staged changes
--name-only show only names of changed files
--name-status show only names and status changes
--raw generate the raw format
--format=<format> specify format for stat summary
--color use color output
--no-color never use color output
-R swap two inputs
-a, --text treat all files as text
--ignore-space-at-eol ignore changes in whitespace at EOL
-b, --ignore-space-change ignore changes in amount of whitespace
-w, --ignore-all-space ignore whitespace when comparing lines
--ignored show ignored files as well
--untracked show untracked files
--patience generate diff using the patience algorithm
--minimal spend extra time to find smallest diff
--stat generate a diffstat
--numstat similar to --stat, but more machine friendly
--shortstat only output last line of --stat
--summary output condensed summary of header info
-M, --find-renames <n> set threshold for findind renames (default 50)
-C, --find-copies <n> set threshold for finding copies (default 50)
--find-copies-harder inspect unmodified files for sources of copies
-B, --break-rewrites break complete rewrite changes into pairs
-U, --unified <n> lints of context to show
--inter-hunk-context <n> maximum lines of change between hunks
--abbrev <n> length to abbreviate commits to
--src-prefix <s> show given source prefix instead of 'a/'
--dst-prefix <s> show given destinction prefix instead of 'b/'
--git-dir <path> path to git repository to use
-h, --help show this message
";

let args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let args = Args::from_args();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
Expand Down
Loading