Skip to content

Commit

Permalink
new: replace structopt with clap 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jun 7, 2022
1 parent 719f45c commit 53020b6
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 168 deletions.
126 changes: 72 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ serde_json = { version = "1", features = ["raw_value"] }
serde_yaml = "0"
shellwords = "1"
signal-hook = "0"
structopt = "0"
thiserror = "1"
wildmatch = "2"

Expand All @@ -51,8 +50,8 @@ version = "1"
default-features = false

[dependencies.clap]
version = "2"
features = ["wrap_help"]
version = "3"
features = ["wrap_help", "derive", "env"]

[dependencies.winapi]
version = "0"
Expand Down
60 changes: 29 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,43 +297,41 @@ Log viewer which translates JSON logs into pretty human-readable representation.
### Complete set of options and flags
```
hl 0.10.3
hl 0.11.0
JSON log converter to human readable representation

USAGE:
hl [FLAGS] [OPTIONS] [--] [FILE]...

FLAGS:
-c Handful alias for --color=always, overrides --color option
--help Prints help information
--list-themes List available themes and exit
-L, --local Use local time zone, overrides --time-zone option
-P Handful alias for --paging=never, overrides --paging option
-r, --raw-fields Disable unescaping and prettifying of field values
-V, --version Prints version information

OPTIONS:
--buffer-size <buffer-size> Buffer size [env: HL_BUFFER_SIZE=] [default: 2 MiB]
--color <color> Color output options, one of { auto, always, never } [env: HL_COLOR=] [default: auto]
-C, --concurrency <concurrency> Number of processing threads [env: HL_CONCURRENCY=]
-f, --filter <filter>... Filtering by field values in one of forms [<key>=<value>, <key>~=<value>, <key>~~=<value>, <key>!=<value>, <key>!~=<value>, <key>!~~=<value>] where ~ denotes substring match and ~~ denotes regular expression match
-h, --hide <hide>... Hide fields with the specified keys
-e, --hide-empty-fields <hide-empty-fields> Hide empty fields, applies for null, string, object and array fields only [env: HL_HIDE_EMPTY_FIELDS=]
--interrupt-ignore-count <interrupt-ignore-count> Number of interrupts to ignore, i.e. Ctrl-C (SIGINT) [env: HL_INTERRUPT_IGNORE_COUNT=] [default: 3]
-l, --level <level> Filtering by level, one of { d[ebug], i[nfo], w[arning], e[rror] } [env: HL_LEVEL=]
--max-message-size <max-message-size> Maximum message size [env: HL_MAX_MESSAGE_SIZE=] [default: 64 MiB]
--paging <paging> Output paging options, one of { auto, always, never } [env: HL_PAGING=] [default: auto]
-H, --show <show>... Hide all fields except fields with the specified keys
-E, --show-empty-fields <show-empty-fields> Show empty fields, overrides --hide-empty-fields option [env: HL_SHOW_EMPTY_FIELDS=]
--since <since> Filtering by timestamp >= the value (--time-zone and --local options are honored)
--theme <theme> Color theme [env: HL_THEME=] [default: one-dark-green]
-t, --time-format <time-format> Time format, see https://man7.org/linux/man-pages/man1/date.1.html [env: HL_TIME_FORMAT=] [default: %b %d %T.%3N]
-Z, --time-zone <time-zone> Time zone name, see column "TZ database name" at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones [env: HL_TIME_ZONE=] [default: UTC]
-u, --unhide <unhide>... Unhide fields with the specified keys
--until <until> Filtering by timestamp <= the value (--time-zone and --local options are honored)
hl [OPTIONS] [--] [FILE]...

ARGS:
<FILE>... Files to process

OPTIONS:
--buffer-size <BUFFER_SIZE> Buffer size [env: HL_BUFFER_SIZE=] [default: "2 MiB"]
-c Handful alias for --color=always, overrides --color option
-C, --concurrency <CONCURRENCY> Number of processing threads [env: HL_CONCURRENCY=]
--color <COLOR> Color output options [env: HL_COLOR=] [default: auto] [possible values: auto, always, never]
-e, --hide-empty-fields Hide empty fields, applies for null, string, object and array fields only [env: HL_HIDE_EMPTY_FIELDS=]
-E, --show-empty-fields Show empty fields, overrides --hide-empty-fields option [env: HL_SHOW_EMPTY_FIELDS=]
-f, --filter <FILTER> Filtering by field values in one of forms [<key>=<value>, <key>~=<value>, <key>~~=<value>, <key>!=<value>, <key>!~=<value>, <key>!~~=<value>] where ~ denotes substring match and ~~ denotes regular expression match
-h, --hide <HIDE> Hide fields with the specified keys
-H, --show <SHOW> Hide all fields except fields with the specified keys
--help Print help information
--interrupt-ignore-count <INTERRUPT_IGNORE_COUNT> Number of interrupts to ignore, i.e. Ctrl-C (SIGINT) [env: HL_INTERRUPT_IGNORE_COUNT=] [default: 3]
-l, --level <LEVEL> Filtering by level [env: HL_LEVEL=] [possible values: error, warning, info, debug]
-L, --local Use local time zone, overrides --time-zone option
--list-themes List available themes and exit
--max-message-size <MAX_MESSAGE_SIZE> Maximum message size [env: HL_MAX_MESSAGE_SIZE=] [default: "64 MiB"]
-P Handful alias for --paging=never, overrides --paging option
--paging <PAGING> Output paging options [env: HL_PAGING=] [default: auto] [possible values: auto, always, never]
-r, --raw-fields Disable unescaping and prettifying of field values
--since <SINCE> Filtering by timestamp >= the value (--time-zone and --local options are honored)
-t, --time-format <TIME_FORMAT> Time format, see https://man7.org/linux/man-pages/man1/date.1.html [env: HL_TIME_FORMAT=] [default: "%y-%m-%d %T.%3N"]
--theme <THEME> Color theme [env: HL_THEME=] [default: one-dark-green]
-u, --unhide <UNHIDE> Unhide fields with the specified keys
--until <UNTIL> Filtering by timestamp <= the value (--time-zone and --local options are honored)
-V, --version Print version information
-Z, --time-zone <TIME_ZONE> Time zone name, see column "TZ database name" at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones [env: HL_TIME_ZONE=] [default: UTC]
```
## Performance
Expand Down
58 changes: 37 additions & 21 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,21 @@ pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
ParseIntError(#[from] ParseIntError),
SizeParseError(#[from] SizeParseError),
#[error(transparent)]
TryFromIntError(#[from] TryFromIntError),
NonZeroSizeParseError(#[from] NonZeroSizeParseError),
#[error("failed to load configuration: {0}")]
Config(#[from] ConfigError),
#[error(transparent)]
Boxed(#[from] Box<dyn std::error::Error + std::marker::Send>),
#[error("file {filename:?} not found")]
FileNotFoundError { filename: String },
#[error("invalid level {value:?}, use any of {valid_values:?}")]
InvalidLevel {
value: String,
valid_values: Vec<String>,
},
#[error("invalid field kind {value:?}, use any of {valid_values:?}")]
InvalidFieldKind {
value: String,
valid_values: Vec<String>,
},
#[error(
"invalid size {0:?}, use {:?} or {:?} format for IEC units or {:?} format for SI units",
"64K",
"64KiB",
"64KB"
)]
InvalidSize(String),
#[error(transparent)]
InvalidLevel(#[from] InvalidLevelError),
#[error("cannot recognize time {0:?}")]
UnrecognizedTime(String),
#[error("unknown theme {name:?}, use any of {known:?}")]
UnknownTheme { name: String, known: Vec<String> },
#[error("zero size")]
ZeroSize,
#[error("failed to parse utf-8 string: {0}")]
Utf8Error(#[from] std::str::Utf8Error),
#[error("failed to parse yaml: {0}")]
Expand All @@ -55,5 +38,38 @@ pub enum Error {
WrongRegularExpression(#[from] regex::Error),
}

/// SizeParseError is an error which may occur when parsing size.
#[derive(Error, Debug)]
pub enum SizeParseError {
#[error(transparent)]
ParseIntError(#[from] ParseIntError),
#[error(transparent)]
TryFromIntError(#[from] TryFromIntError),
#[error(
"invalid size {0:?}, use {:?} or {:?} format for IEC units or {:?} format for SI units",
"64K",
"64KiB",
"64KB"
)]
InvalidSize(String),
}

/// NonZeroSizeParseError is an error which may occur when parsing non-zero size.
#[derive(Error, Debug)]
pub enum NonZeroSizeParseError {
#[error(transparent)]
SizeParseError(#[from] SizeParseError),
#[error("zero size")]
ZeroSize,
}

/// NonZeroSizeParseError is an error which may occur when parsing non-zero size.
#[derive(Error, Debug)]
#[error("invalid level {value:?}, use any of {valid_values:?}")]
pub struct InvalidLevelError {
pub value: String,
pub valid_values: Vec<String>,
}

/// Result is an alias for standard result with bound Error type.
pub type Result<T> = std::result::Result<T, Error>;
Loading

0 comments on commit 53020b6

Please sign in to comment.