From cecd2f66a5d6a6b44f27f8ca499e359d82d29ab7 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 22 Apr 2020 00:28:07 -0700 Subject: [PATCH] Fix help strings Fix inconsistent argument value names in help strings. type: fixed --- CHANGELOG.md | 3 +- src/subcommand/torrent/create.rs | 8 +-- src/subcommand/torrent/link.rs | 110 +++++++++++++++++++++++++++++-- src/subcommand/torrent/verify.rs | 6 +- src/test_env.rs | 16 ++++- 5 files changed, 126 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13f4e84..862b555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ Changelog UNRELEASED - 2020-04-22 ----------------------- -- :zap: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Allow positional shell to `imdl completions` - Fixes [#375](https://github.com/casey/intermodal/issues/375) - _Casey Rodarmor _ +- :bug: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Fix help strings - _Casey Rodarmor _ +- :zap: [`ebec2d591a7a`](https://github.com/casey/intermodal/commit/ebec2d591a7a0e2a2c4cd55217db4ba46b5dd9ed) Allow positional shell to `imdl completions` - Fixes [#375](https://github.com/casey/intermodal/issues/375) - _Casey Rodarmor _ - :art: [`134c241ae7f8`](https://github.com/casey/intermodal/commit/134c241ae7f8e374d8a9266e7eb0c4a9c3844c30) Use `lexiclean` crate for lexical path cleaning - _Casey Rodarmor _ - :zap: [`323434d0aa21`](https://github.com/casey/intermodal/commit/323434d0aa21ebfda5be85ecd4a38a55ed3fec0a) Allow positional input to `imdl torrent verify` - Fixes [#375](https://github.com/casey/intermodal/issues/375) - _Casey Rodarmor _ - :zap: [`5ba885dbc4f2`](https://github.com/casey/intermodal/commit/5ba885dbc4f24781d6a3240ddfc0c03177b12f1e) Take input to `imdl torrent create` as positional - Fixes [#375](https://github.com/casey/intermodal/issues/375) - _Casey Rodarmor _ diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index 379dafc..3088851 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -5,9 +5,9 @@ use create_step::CreateStep; mod create_content; mod create_step; -const INPUT_HELP: &str = "Read torrent contents from `PATH`. If `PATH` is a file, torrent will be \ - a single-file torrent. If `PATH` is a directory, torrent will be a \ - multi-file torrent. If `PATH` is `-`, read from standard input. Piece \ +const INPUT_HELP: &str = "Read torrent contents from `INPUT`. If `INPUT` is a file, torrent will \ + be a single-file torrent. If `INPUT` is a directory, torrent will be a \ + multi-file torrent. If `INPUT` is `-`, read from standard input. Piece \ length defaults to 256KiB when reading from standard input if \ `--piece-length` is not given."; @@ -138,7 +138,7 @@ Examples: name = INPUT_FLAG, long = "input", short = "i", - value_name = "PATH", + value_name = "INPUT", empty_values = false, parse(try_from_os_str = InputTarget::try_from_os_str), help = INPUT_HELP, diff --git a/src/subcommand/torrent/link.rs b/src/subcommand/torrent/link.rs index c618235..a5cfbbd 100644 --- a/src/subcommand/torrent/link.rs +++ b/src/subcommand/torrent/link.rs @@ -1,5 +1,12 @@ use crate::common::*; +const INPUT_HELP: &str = "Generate magnet link from metainfo at `INPUT`. If `INPUT` is `-`, read \ + metainfo from standard input."; + +const INPUT_FLAG: &str = "input-flag"; + +const INPUT_POSITIONAL: &str = ""; + #[derive(StructOpt)] #[structopt( help_message(consts::HELP_MESSAGE), @@ -8,15 +15,25 @@ use crate::common::*; )] pub(crate) struct Link { #[structopt( + name = INPUT_FLAG, long = "input", short = "i", - value_name = "METAINFO", + value_name = "INPUT", empty_values(false), parse(try_from_os_str = InputTarget::try_from_os_str), - help = "Generate magnet link from metainfo at `PATH`. If `PATH` is `-`, read metainfo from \ - standard input.", + help = INPUT_HELP, )] - input: InputTarget, + input_flag: Option, + #[structopt( + name = INPUT_POSITIONAL, + value_name = "INPUT", + empty_values(false), + parse(try_from_os_str = InputTarget::try_from_os_str), + required_unless = INPUT_FLAG, + conflicts_with = INPUT_FLAG, + help = INPUT_HELP, + )] + input_positional: Option, #[structopt( long = "open", short = "O", @@ -44,7 +61,14 @@ pub(crate) struct Link { impl Link { pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> { - let input = env.read(self.input.clone())?; + let input = xor_args( + "input_flag", + &self.input_flag, + "input_positional", + &self.input_positional, + )?; + + let input = env.read(input.clone())?; let infohash = Infohash::from_input(&input)?; let metainfo = Metainfo::from_input(&input)?; @@ -84,7 +108,56 @@ mod tests { use pretty_assertions::assert_eq; #[test] - fn no_announce() { + fn input_required() { + test_env! { + args: [ + "torrent", + "link", + ], + tree: { + }, + matches: Err(Error::Clap { .. }), + }; + + test_env! { + args: [ + "torrent", + "link", + "--input", + "foo", + ], + tree: { + }, + matches: Err(Error::Filesystem { .. }), + }; + + test_env! { + args: [ + "torrent", + "link", + "foo", + ], + tree: { + }, + matches: Err(Error::Filesystem { .. }), + }; + + test_env! { + args: [ + "torrent", + "link", + "--input", + "foo", + "foo", + ], + tree: { + }, + matches: Err(Error::Clap { .. }), + }; + } + + #[test] + fn no_announce_flag() { let mut env = test_env! { args: [ "torrent", @@ -109,6 +182,31 @@ mod tests { ); } + #[test] + fn no_announce_positional() { + let mut env = test_env! { + args: [ + "torrent", + "link", + "foo.torrent", + ], + tree: { + "foo.torrent": "d4:infod6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:ee", + } + }; + + env.assert_ok(); + + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + + let infohash = Sha1Digest::from_data(INFO.as_bytes()); + + assert_eq!( + env.out(), + format!("magnet:?xt=urn:btih:{}&dn=foo\n", infohash), + ); + } + #[test] fn with_announce() { let mut env = test_env! { diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index 7ce04bb..e100f0d 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -3,8 +3,8 @@ use verify_step::VerifyStep; mod verify_step; -const METAINFO_HELP: &str = "Verify torrent contents against torrent metainfo in `METAINFO`. If \ - `METAINFO` is `-`, read metainfo from standard input."; +const METAINFO_HELP: &str = "Verify torrent contents against torrent metainfo in `INPUT`. If \ + `INPUT` is `-`, read metainfo from standard input."; const INPUT_POSITIONAL: &str = ""; @@ -41,7 +41,7 @@ pub(crate) struct Verify { name = INPUT_FLAG, long = "input", short = "i", - value_name = "METAINFO", + value_name = "INPUT", empty_values = false, parse(try_from_os_str = InputTarget::try_from_os_str), help = METAINFO_HELP, diff --git a/src/test_env.rs b/src/test_env.rs index a2bf544..18ca2fb 100644 --- a/src/test_env.rs +++ b/src/test_env.rs @@ -8,19 +8,29 @@ macro_rules! test_env { $(err_style: $err_style:expr,)? tree: { $($tree:tt)* - } $(,)? + } + $(, matches: $result:pat)? + + $(,)? } => { { let tempdir = temptree! { $($tree)* }; - TestEnvBuilder::new() + let env = TestEnvBuilder::new() $(.current_dir(tempdir.path().join($cwd)))? $(.err_style($err_style))? $(.input($input))? .tempdir(tempdir) .arg("imdl") $(.arg($arg))* - .build() + .build(); + + $( + let mut env = env; + assert_matches!(env.run(), $result); + )? + + env } } }