summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsitkevij <1553398+sitkevij@users.noreply.github.com>2023-11-19 23:25:22 -0800
committersitkevij <1553398+sitkevij@users.noreply.github.com>2023-11-19 23:25:22 -0800
commit2affa4966c2c7a9ec1fa0c5fe5ff11656f9f7537 (patch)
treeb4d50b7551c0da02ead395edce2f33cada112a7f
parent6d07f194473714a40885386c442ab808fab30af3 (diff)
feat: increment project version, new clap implementation, improved error handlingv0.5.0
-rw-r--r--.gitignore1
-rw-r--r--.markdownlint.json15
-rw-r--r--.markdownlint.yaml29
-rw-r--r--.rusty-hook.toml5
-rw-r--r--Cargo.lock379
-rw-r--r--Cargo.toml9
-rw-r--r--Makefile19
-rw-r--r--src/lib.rs52
-rw-r--r--src/main.rs66
-rwxr-xr-xtests.sh14
-rwxr-xr-xtests/files/tests.sh15
11 files changed, 461 insertions, 143 deletions
diff --git a/.gitignore b/.gitignore
index f366e44..bb5210a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,7 @@
.DS_Store
.vscode
*.ignore.*
+*.ignore
# ci code coverage
tarpaulin-report.html
diff --git a/.markdownlint.json b/.markdownlint.json
new file mode 100644
index 0000000..aed3162
--- /dev/null
+++ b/.markdownlint.json
@@ -0,0 +1,15 @@
+{
+ "MD013": {
+ "line_length": 100,
+ "heading_line_length": 100,
+ "code_block_line_length": 100,
+ "code_blocks": false,
+ "tables": false,
+ "headings": true,
+ "headers": true,
+ "strict": false,
+ "stern": false
+ },
+ "MD025": false,
+ "MD034": false
+}
diff --git a/.markdownlint.yaml b/.markdownlint.yaml
deleted file mode 100644
index 300857e..0000000
--- a/.markdownlint.yaml
+++ /dev/null
@@ -1,29 +0,0 @@
-# MD013/line-length - Line length
-MD013:
- # Number of characters
- line_length: 100
- # Number of characters for headings
- heading_line_length: 100
- # Number of characters for code blocks
- code_block_line_length: 100
- # Include code blocks
- code_blocks: false
- # Include tables
- tables: false
- # Include headings
- headings: true
- # Include headings
- headers: true
- # Strict length checking
- strict: false
- # Stern length checking
- stern: false
-
-# set for man page markdown
-MD025: false
-# MD025/single-title/single-h1 - Multiple top-level headings in the same document
-# MD025:
-# # Heading level
-# level: 1
-# # RegExp for matching title in front matter
-# front_matter_title: "^\\s*title\\s*[:=]"
diff --git a/.rusty-hook.toml b/.rusty-hook.toml
new file mode 100644
index 0000000..96caa0c
--- /dev/null
+++ b/.rusty-hook.toml
@@ -0,0 +1,5 @@
+[hooks]
+pre-commit = "cargo fmt --all --verbose && markdownlint-cli2 --config .markdownlint.json *.md && codespell -L crate -w src/*.rs *.md tests/*.rs *.toml && cargo test && cargo clippy"
+
+[logging]
+verbose = true
diff --git a/Cargo.lock b/Cargo.lock
index 7343a34..db159e8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,29 +1,72 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
+version = 3
+
[[package]]
name = "ansi_term"
-version = "0.11.0"
+version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
+checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
-name = "ansi_term"
-version = "0.12.1"
+name = "anstream"
+version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
+checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
dependencies = [
- "winapi",
+ "anstyle",
+ "anstyle-parse",
+ "anstyle-query",
+ "anstyle-wincon",
+ "colorchoice",
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
+
+[[package]]
+name = "anstyle-parse"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
+dependencies = [
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle-query"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
+dependencies = [
+ "windows-sys",
+]
+
+[[package]]
+name = "anstyle-wincon"
+version = "3.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
+dependencies = [
+ "anstyle",
+ "windows-sys",
]
[[package]]
name = "assert_cmd"
-version = "1.0.1"
+version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c88b9ca26f9c16ec830350d309397e74ee9abdfd8eb1f71cb6ecc71a3fc818da"
+checksum = "88903cb14723e4d4003335bb7f8a14f27691649105346a0f0957466c096adfe6"
dependencies = [
+ "anstyle",
+ "bstr",
"doc-comment",
"predicates",
"predicates-core",
@@ -43,31 +86,69 @@ dependencies = [
]
[[package]]
-name = "bitflags"
-version = "1.0.4"
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
+[[package]]
+name = "bstr"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "542f33a8835a0884b006a0c3df3dadd99c0c3f296ed26c2fdc8028e01ad6230c"
+dependencies = [
+ "memchr",
+ "regex-automata",
+ "serde",
+]
+
+[[package]]
+name = "ci_info"
+version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
+checksum = "24f638c70e8c5753795cc9a8c07c44da91554a09e4cf11a7326e8161b0a3c45e"
+dependencies = [
+ "envmnt",
+]
[[package]]
name = "clap"
-version = "2.33.3"
+version = "4.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
+checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64"
dependencies = [
- "ansi_term 0.11.0",
- "atty",
- "bitflags",
+ "clap_builder",
+]
+
+[[package]]
+name = "clap_builder"
+version = "4.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc"
+dependencies = [
+ "anstream",
+ "anstyle",
+ "clap_lex",
"strsim",
- "textwrap",
- "unicode-width",
- "vec_map",
]
[[package]]
-name = "difference"
-version = "2.0.0"
+name = "clap_lex"
+version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
+checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
+
+[[package]]
+name = "colorchoice"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
+
+[[package]]
+name = "difflib"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]]
name = "doc-comment"
@@ -76,14 +157,71 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
+name = "either"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
+
+[[package]]
+name = "envmnt"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a2d328fc287c61314c4a61af7cfdcbd7e678e39778488c7cb13ec133ce0f4059"
+dependencies = [
+ "fsio",
+ "indexmap",
+]
+
+[[package]]
+name = "fsio"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1fd087255f739f4f1aeea69f11b72f8080e9c2e7645cd06955dad4a178a49e3"
+
+[[package]]
+name = "getopts"
+version = "0.2.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "hashbrown"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
+
+[[package]]
name = "hx"
-version = "0.4.2"
+version = "0.5.0"
dependencies = [
- "ansi_term 0.12.1",
+ "ansi_term",
"assert_cmd",
"atty",
"clap",
"no_color",
+ "rusty-hook",
+]
+
+[[package]]
+name = "indexmap"
+version = "1.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
+dependencies = [
+ "autocfg",
+ "hashbrown",
+]
+
+[[package]]
+name = "itertools"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
+dependencies = [
+ "either",
]
[[package]]
@@ -93,6 +231,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48450664a984b25d5b479554c29cc04e3150c97aa4c01da5604a2d4ed9151476"
[[package]]
+name = "memchr"
+version = "2.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
+
+[[package]]
+name = "nias"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ab250442c86f1850815b5d268639dff018c0627022bc1940eb2d642ca1ce12f0"
+
+[[package]]
name = "no_color"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -100,28 +250,48 @@ checksum = "bdf5e47de39a83b44018b2ee76a7a2643f37738ee44da99e145bc9804b341dd4"
[[package]]
name = "predicates"
-version = "1.0.5"
+version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "96bfead12e90dccead362d62bb2c90a5f6fc4584963645bc7f71a735e0b0735a"
+checksum = "6dfc28575c2e3f19cb3c73b93af36460ae898d426eba6fc15b9bd2a5220758a0"
dependencies = [
- "difference",
+ "anstyle",
+ "difflib",
+ "itertools",
"predicates-core",
]
[[package]]
name = "predicates-core"
-version = "1.0.0"
+version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178"
+checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174"
[[package]]
name = "predicates-tree"
-version = "1.0.0"
+version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124"
+checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf"
dependencies = [
"predicates-core",
- "treeline",
+ "termtree",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.33"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
+dependencies = [
+ "proc-macro2",
]
[[package]]
@@ -140,10 +310,59 @@ dependencies = [
]
[[package]]
+name = "regex-automata"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
+
+[[package]]
+name = "rusty-hook"
+version = "0.11.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96cee9be61be7e1cbadd851e58ed7449c29c620f00b23df937cb9cbc04ac21a3"
+dependencies = [
+ "ci_info",
+ "getopts",
+ "nias",
+ "toml",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.192"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.192"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "strsim"
-version = "0.8.0"
+version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+
+[[package]]
+name = "syn"
+version = "2.0.39"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
[[package]]
name = "termion"
@@ -157,19 +376,25 @@ dependencies = [
]
[[package]]
-name = "textwrap"
-version = "0.11.0"
+name = "termtree"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76"
+
+[[package]]
+name = "toml"
+version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
- "unicode-width",
+ "serde",
]
[[package]]
-name = "treeline"
-version = "0.1.0"
+name = "unicode-ident"
+version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"
+checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-width"
@@ -178,10 +403,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
[[package]]
-name = "vec_map"
-version = "0.8.1"
+name = "utf8parse"
+version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
+checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "wait-timeout"
@@ -213,3 +438,69 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
+name = "windows-sys"
+version = "0.48.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc",
+ "windows_i686_gnu",
+ "windows_i686_msvc",
+ "windows_x86_64_gnu",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc",
+]
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.48.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
diff --git a/Cargo.toml b/Cargo.toml
index 5390795..d276b3f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,8 +8,8 @@ include = ["src/**/*", "Cargo.toml", "Cargo.lock", "README.md", "hx.1"]
license = "MIT"
name = "hx"
readme = "README.md"
-version = "0.4.2"
-edition = "2018"
+version = "0.5.0"
+edition = "2021"
# see https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -26,10 +26,11 @@ panic = 'unwind' # panic strategy (`-C panic=...`), can also be 'abort'
opt-level = 3
[dependencies]
-clap = "2.33"
+clap = "4.4.8"
ansi_term = "0.12"
atty = "0.2"
no_color = "0.1"
[dev-dependencies]
-assert_cmd = "1.0.1"
+assert_cmd = "2.0.12"
+rusty-hook = "^0.11.2"
diff --git a/Makefile b/Makefile
index 41cfaa7..41c4eba 100644
--- a/Makefile
+++ b/Makefile
@@ -77,9 +77,6 @@ install: release debug test
install-force: clean release debug test
cargo install --path . --force
-clippy:
- cargo clippy
-
docker-build:
docker build -t sitkevij/hx:latest .
@@ -100,6 +97,22 @@ manpage:
cp hx.1 /usr/local/share/man/man1
man hx
+lint: lint-clippy lint-format lint-markdown lint-spell
+
+lint-clippy:
+ cargo clippy --workspace --all-targets --verbose
+ cargo clippy --workspace --all-targets --verbose --no-default-features
+ cargo clippy --workspace --all-targets --verbose --all-features
+
+lint-format:
+ cargo fmt --all -- --check
+
+lint-markdown:
+ markdownlint-cli2 --config .markdownlint.json *.md
+
+lint-spell:
+ codespell -L crate -w src/*.rs *.md tests/*.rs *.toml
+
clean: ## Remove all artifacts
rm -rf $(DEBUG_DIR)
rm -rf $(RELEASE_DIR)
diff --git a/src/lib.rs b/src/lib.rs
index 9a02fb4..44490dc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -247,10 +247,16 @@ pub fn append_ascii(target: &mut Vec<u8>, b: u8, colorize: bool) {
pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> {
let mut column_width: u64 = 10;
let mut truncate_len: u64 = 0x0;
- if let Some(len) = matches.value_of("func") {
+ if let Some(len) = matches.get_one::<String>("func") {
let mut p: usize = 4;
- if let Some(places) = matches.value_of("places") {
- p = places.parse::<usize>().unwrap();
+ if let Some(places) = matches.get_one::<String>("places") {
+ p = match places.parse::<usize>() {
+ Ok(p) => p,
+ Err(e) => {
+ eprintln!("-p, --places <integer> expected. {:?}", e);
+ return Err(Box::new(e));
+ }
+ }
}
output_function(len.parse::<u64>().unwrap(), p);
} else {
@@ -264,23 +270,35 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> {
Box::new(BufReader::new(io::stdin()))
} else {
Box::new(BufReader::new(fs::File::open(
- matches.value_of(ARG_INP).unwrap(),
+ matches.get_one::<String>(ARG_INP).unwrap(),
)?))
};
let mut format_out = Format::LowerHex;
let mut colorize = true;
- if let Some(columns) = matches.value_of(ARG_COL) {
- column_width = columns.parse::<u64>().unwrap(); //turbofish
+ if let Some(columns) = matches.get_one::<String>(ARG_COL) {
+ column_width = match columns.parse::<u64>() {
+ Ok(column_width) => column_width,
+ Err(e) => {
+ eprintln!("-c, --cols <integer> expected. {:?}", e);
+ return Err(Box::new(e));
+ }
+ }
}
- if let Some(length) = matches.value_of(ARG_LEN) {
- truncate_len = length.parse::<u64>()?;
+ if let Some(length) = matches.get_one::<String>(ARG_LEN) {
+ truncate_len = match length.parse::<u64>() {
+ Ok(truncate_len) => truncate_len,
+ Err(e) => {
+ eprintln!("-l, --len <integer> expected. {:?}", e);
+ return Err(Box::new(e));
+ }
+ }
}
- if let Some(format) = matches.value_of(ARG_FMT) {
+ if let Some(format) = matches.get_one::<String>(ARG_FMT) {
// o, x, X, p, b, e, E
- match format {
+ match format.as_str() {
"o" => format_out = Format::Octal,
"x" => format_out = Format::LowerHex,
"X" => format_out = Format::UpperHex,
@@ -305,13 +323,12 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> {
colorize = false;
}
- if let Some(color) = matches.value_of(ARG_CLR) {
- let color_v = color.parse::<u8>().unwrap();
- colorize = color_v == 1;
+ if let Some(color) = matches.get_one::<String>(ARG_CLR) {
+ colorize = color.parse::<u8>().unwrap() == 1;
}
// array output mode is mutually exclusive
- if let Some(array) = matches.value_of(ARG_ARR) {
+ if let Some(array) = matches.get_one::<String>(ARG_ARR) {
output_array(array, buf, truncate_len, column_width)?;
} else {
// Transforms this Read instance to an Iterator over its bytes.
@@ -368,10 +385,7 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> {
#[allow(clippy::absurd_extreme_comparisons)]
pub fn is_stdin(matches: ArgMatches) -> Result<bool, Box<dyn Error>> {
let mut is_stdin = false;
- if DBG > 0 {
- dbg!(env::args().len(), matches.args.len());
- }
- if let Some(file) = matches.value_of(ARG_INP) {
+ if let Some(file) = matches.get_one::<String>(ARG_INP) {
if DBG > 0 {
dbg!(file);
}
@@ -381,7 +395,7 @@ pub fn is_stdin(matches: ArgMatches) -> Result<bool, Box<dyn Error>> {
dbg!(nth1);
}
is_stdin = ARGS.iter().any(|arg| matches.index_of(arg) == Some(2));
- } else if matches.args.is_empty() {
+ } else if !matches.args_present() {
is_stdin = true;
}
if DBG > 0 {
diff --git a/src/main.rs b/src/main.rs
index 2021fc5..769fe98 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,7 @@
extern crate clap;
-use clap::{App, Arg};
+use clap::Arg;
+use clap::Command;
use std::env;
use std::io::Error;
use std::io::ErrorKind;
@@ -8,76 +9,81 @@ use std::process;
/// Central application entry point.
fn main() {
- let desc: &str = &format!(
+ let desc = &format!(
"{}\n{}",
env!("CARGO_PKG_DESCRIPTION"),
env!("CARGO_PKG_HOMEPAGE")
);
- let app = App::new(env!("CARGO_PKG_NAME"))
+ let app = Command::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.about(desc)
.arg(
- Arg::with_name(hx::ARG_COL)
- .short("c")
+ Arg::new(hx::ARG_COL)
+ .action(clap::ArgAction::Set)
+ .short('c')
.long(hx::ARG_COL)
.value_name("columns")
.help("Set column length")
- .takes_value(true),
+ .num_args(1)
)
.arg(
- Arg::with_name(hx::ARG_LEN)
- .short("l")
+ Arg::new(hx::ARG_LEN)
+ .action(clap::ArgAction::Set)
+ .short('l')
.long(hx::ARG_LEN)
.value_name(hx::ARG_LEN)
.help("Set <len> bytes to read")
- .takes_value(true),
+ .num_args(1)
)
.arg(
- Arg::with_name(hx::ARG_FMT)
- .short("f")
+ Arg::new(hx::ARG_FMT)
+ .action(clap::ArgAction::Set)
+ .short('f')
.long(hx::ARG_FMT)
.help("Set format of octet: Octal (o), LowerHex (x), UpperHex (X), Binary (b)")
- .possible_values(&["o", "x", "X", "b"])
- .takes_value(true),
+ .value_parser(["o", "x", "X", "b"])
+ .num_args(1)
)
.arg(
- Arg::with_name(hx::ARG_INP)
+ Arg::new(hx::ARG_INP)
.help("Pass file path as an argument, or input data may be passed via stdin")
.required(false)
.index(1),
)
.arg(
- Arg::with_name(hx::ARG_CLR)
- .short("t")
+ Arg::new(hx::ARG_CLR)
+ .action(clap::ArgAction::Set)
+ .short('t')
.long(hx::ARG_CLR)
.help("Set color tint terminal output. 0 to disable, 1 to enable")
- .possible_values(&["0", "1"])
- .takes_value(true),
+ .value_parser(["0", "1"])
+ .num_args(1)
)
.arg(
- Arg::with_name(hx::ARG_ARR)
- .short("a")
+ Arg::new(hx::ARG_ARR)
+ .action(clap::ArgAction::Set)
+ .short('a')
.long(hx::ARG_ARR)
.value_name("array_format")
.help("Set source code format output: rust (r), C (c), golang (g), python (p), kotlin (k), java (j), swift (s), fsharp (f)")
- .possible_values(&["r", "c", "g", "p", "k", "j", "s", "f"])
- .takes_value(true),
+ .value_parser(["r", "c", "g", "p", "k", "j", "s", "f"])
+ .num_args(1)
)
.arg(
- Arg::with_name(hx::ARG_FNC)
- .short("u")
+ Arg::new(hx::ARG_FNC)
+ .short('u')
.long(hx::ARG_FNC)
.value_name("func_length")
.help("Set function wave length")
- .takes_value(true),
+ .num_args(1)
)
.arg(
- Arg::with_name(hx::ARG_PLC)
- .short("p")
+ Arg::new(hx::ARG_PLC)
+ .short('p')
.long(hx::ARG_PLC)
.value_name("func_places")
.help("Set function wave output decimal places")
- .takes_value(true),
+ .num_args(1)
);
let matches = app.get_matches();
@@ -85,14 +91,14 @@ fn main() {
Ok(_) => {
process::exit(0);
}
- Err(_) => {
+ Err(e) => {
let err = &Error::last_os_error();
let suppress_error = match err.kind() {
ErrorKind::BrokenPipe => process::exit(0),
_ => false,
};
if !suppress_error {
- eprintln!("error: {}", err);
+ eprintln!("error: {}", e);
process::exit(1);
}
}
diff --git a/tests.sh b/tests.sh
deleted file mode 100755
index b6782cd..0000000
--- a/tests.sh
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-
-# param ordering 1
-target/debug/hx -ar tests/files/tiny.txt
-# param ordering 2
-target/debug/hx tests/files/tiny.txt -ar
-# binary output column width 4
-target/debug/hx -c4 -fb tests/files/alphanumeric.txt
-# missing len param
-target/debug/hx --len tests/files/tiny.txt
-# missing file name
-target/debug/hx missing-file
-# simulate broken pipe
-dd if=/dev/random bs=512 count=10 | RUST_BACKTRACE=1 target/debug/hx | head -n 10
diff --git a/tests/files/tests.sh b/tests/files/tests.sh
new file mode 100755
index 0000000..2bf80b0
--- /dev/null
+++ b/tests/files/tests.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+HX_BIN="../../target/debug/hx"
+# param ordering 1
+$HX_BIN -ar tiny.txt
+# param ordering 2
+$HX_BIN tiny.txt -ar
+# binary output column width 4
+$HX_BIN -c4 -fb alphanumeric.txt
+# missing len param
+$HX_BIN --len tiny.txt
+# missing file name
+# $HX_BIN missing-file
+# simulate broken pipe
+dd if=/dev/random bs=512 count=10 | RUST_BACKTRACE=1 $HX_BIN | head -n 10