From ac7cac29106fd03219b753dcc0a1419b6e520c4d Mon Sep 17 00:00:00 2001 From: Gaurang Tandon <1gaurangtandon@gmail.com> Date: Tue, 12 Jan 2021 22:14:11 +0530 Subject: [PATCH] tail: moving to clap from getopt (#1689) --- Cargo.lock | 106 ++++++++++++++++++---- src/uu/tail/Cargo.toml | 2 +- src/uu/tail/src/tail.rs | 196 +++++++++++++++++++--------------------- 3 files changed, 179 insertions(+), 125 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3814de008..70f39ae3f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -103,7 +103,7 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "regex-automata 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -210,7 +210,7 @@ dependencies = [ "rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "unindent 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -406,9 +406,9 @@ dependencies = [ "plotters 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", "serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)", "tinytemplate 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -474,7 +474,7 @@ dependencies = [ "csv-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -580,6 +580,16 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "getrandom" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "wasi 0.10.1+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glob" version = "0.2.11" @@ -918,6 +928,17 @@ dependencies = [ "rand_pcg 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_chacha" version = "0.2.2" @@ -927,6 +948,15 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ppv-lite86 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_core" version = "0.3.1" @@ -948,6 +978,14 @@ dependencies = [ "getrandom 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_core" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_hc" version = "0.2.0" @@ -956,6 +994,14 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_pcg" version = "0.2.1" @@ -992,6 +1038,14 @@ name = "redox_syscall" version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "redox_syscall" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "redox_termios" version = "0.1.1" @@ -1083,7 +1137,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.118" +version = "1.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1092,12 +1146,12 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "half 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive" -version = "1.0.118" +version = "1.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1112,7 +1166,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "itoa 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1168,13 +1222,13 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", - "redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "remove_dir_all 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1269,7 +1323,7 @@ name = "tinytemplate" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1758,7 +1812,7 @@ version = "0.0.2" dependencies = [ "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.5", "uucore_procs 0.0.5", ] @@ -2043,7 +2097,7 @@ name = "uu_stdbuf" version = "0.0.2" dependencies = [ "getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "uu_stdbuf_libstdbuf 0.0.2", "uucore 0.0.5", "uucore_procs 0.0.5", @@ -2093,7 +2147,7 @@ dependencies = [ name = "uu_tail" version = "0.0.2" dependencies = [ - "getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.5", @@ -2337,6 +2391,11 @@ name = "wasi" version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wasi" +version = "0.10.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasm-bindgen" version = "0.2.69" @@ -2505,6 +2564,7 @@ dependencies = [ "checksum generic-array 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fceb69994e330afed50c93524be68c42fa898c2d9fd4ee8da03bd7363acd26f2" "checksum getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" "checksum getrandom 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +"checksum getrandom 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" "checksum half 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d36fab90f82edc3c747f9d438e06cf0a491055896f2a279638bb5beed6c40177" @@ -2551,15 +2611,20 @@ dependencies = [ "checksum quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" "checksum rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9" "checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +"checksum rand 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c24fcd450d3fa2b592732565aa4f17a27a61c65ece4726353e000939b0edee34" "checksum rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +"checksum rand_chacha 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" "checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +"checksum rand_core 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum rand_hc 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" "checksum rand_pcg 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" "checksum rayon 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" "checksum rayon-core 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" "checksum redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)" = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +"checksum redox_syscall 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "05ec8ca9416c5ea37062b502703cd7fcb207736bc294f6e0cf367ac6fc234570" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" "checksum regex-automata 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" @@ -2573,9 +2638,9 @@ dependencies = [ "checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)" = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +"checksum serde 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)" = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3" "checksum serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" -"checksum serde_derive 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)" = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" +"checksum serde_derive 1.0.119 (registry+https://github.com/rust-lang/crates.io-index)" = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" "checksum serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)" = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum sha2 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d963c78ce367df26d7ea8b8cc655c651b42e8a1e584e869c1e17dae3ccb116a" @@ -2583,7 +2648,7 @@ dependencies = [ "checksum smallvec 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)" = "b97fcaeba89edba30f044a10c6a3cc39df9c3f17d7cd829dd1446cab35f890e0" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum syn 1.0.58 (registry+https://github.com/rust-lang/crates.io-index)" = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" -"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +"checksum tempfile 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" "checksum term_grid 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "230d3e804faaed5a39b08319efb797783df2fd9671b39b7596490cb486d702cf" "checksum term_size 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" "checksum termion 1.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c22cec9d8978d906be5ac94bceb5a010d885c626c4c8855721a4dbd20e3ac905" @@ -2604,6 +2669,7 @@ dependencies = [ "checksum vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +"checksum wasi 0.10.1+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "93c6c3420963c5c64bca373b25e77acb562081b9bb4dd5bb864187742186cea9" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum wasm-bindgen 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)" = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e" "checksum wasm-bindgen-backend 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)" = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62" diff --git a/src/uu/tail/Cargo.toml b/src/uu/tail/Cargo.toml index b7a00cf69..74afa4ae8 100644 --- a/src/uu/tail/Cargo.toml +++ b/src/uu/tail/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" path = "src/tail.rs" [dependencies] -getopts = "0.2.18" +clap = "2.33" libc = "0.2.42" uucore = { version=">=0.0.5", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index cca70ee0e..6d80172cc 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -9,23 +9,35 @@ // spell-checker:ignore (ToDO) seekable seek'd tail'ing ringbuffer ringbuf +#[macro_use] +extern crate clap; + #[macro_use] extern crate uucore; mod platform; +use clap::{App, Arg}; use std::collections::VecDeque; use std::error::Error; use std::fmt; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, Read, Seek, SeekFrom, Write}; use std::path::Path; -use std::str::from_utf8; use std::thread::sleep; use std::time::Duration; -static NAME: &str = "tail"; -static VERSION: &str = env!("CARGO_PKG_VERSION"); +static OPT_BYTES: &str = "bytes"; +static OPT_FOLLOW: &str = "follow"; +static OPT_LINES: &str = "lines"; +static OPT_PID: &str = "pid"; +static OPT_QUIET: &str = "quiet"; +static OPT_SILENT: &str = "silent"; +static OPT_SLEEP_INT: &str = "sleep-interval"; +static OPT_VERBOSE: &str = "verbose"; +static OPT_ZERO_TERM: &str = "zero-terminated"; + +static ARG_FILES: &str = "files"; enum FilterMode { Bytes(u64), @@ -54,65 +66,78 @@ impl Default for Settings { #[allow(clippy::cognitive_complexity)] pub fn uumain(args: impl uucore::Args) -> i32 { - let args = args.collect_str(); - let mut settings: Settings = Default::default(); - // handle obsolete -number syntax - let options = match obsolete(&args[1..]) { - (args, Some(n)) => { - settings.mode = FilterMode::Lines(n, b'\n'); - args - } - (args, None) => args, - }; + let app = App::new(executable!()) + .version(crate_version!()) + .about("output the last part of files") + .arg( + Arg::with_name(OPT_BYTES) + .short("c") + .long(OPT_BYTES) + .takes_value(true) + .help("Number of bytes to print"), + ) + .arg( + Arg::with_name(OPT_FOLLOW) + .short("f") + .long(OPT_FOLLOW) + .help("Print the file as it grows"), + ) + .arg( + Arg::with_name(OPT_LINES) + .short("n") + .long(OPT_LINES) + .takes_value(true) + .help("Number of lines to print"), + ) + .arg( + Arg::with_name(OPT_PID) + .long(OPT_PID) + .takes_value(true) + .help("with -f, terminate after process ID, PID dies"), + ) + .arg( + Arg::with_name(OPT_QUIET) + .short("q") + .long(OPT_QUIET) + .help("never output headers giving file names"), + ) + .arg( + Arg::with_name(OPT_SILENT) + .long(OPT_SILENT) + .help("synonym of --quiet"), + ) + .arg( + Arg::with_name(OPT_SLEEP_INT) + .short("s") + .long(OPT_SLEEP_INT) + .help("Number or seconds to sleep between polling the file when running with -f"), + ) + .arg( + Arg::with_name(OPT_VERBOSE) + .short("v") + .long(OPT_VERBOSE) + .help("always output headers giving file names"), + ) + .arg( + Arg::with_name(OPT_ZERO_TERM) + .short("z") + .long(OPT_ZERO_TERM) + .help("Line delimiter is NUL, not newline"), + ) + .arg( + Arg::with_name(ARG_FILES) + .multiple(true) + .takes_value(true) + .min_values(1), + ); - let args = options; + let matches = app.get_matches_from(args); - let mut opts = getopts::Options::new(); - - opts.optopt("c", "bytes", "Number of bytes to print", "k"); - opts.optopt("n", "lines", "Number of lines to print", "k"); - opts.optflag("f", "follow", "Print the file as it grows"); - opts.optopt( - "s", - "sleep-interval", - "Number or seconds to sleep between polling the file when running with -f", - "n", - ); - opts.optopt( - "", - "pid", - "with -f, terminate after process ID, PID dies", - "PID", - ); - opts.optflag("z", "zero-terminated", "Line delimiter is NUL, not newline"); - opts.optflag("h", "help", "help"); - opts.optflag("V", "version", "version"); - opts.optflag("v", "verbose", "always output headers giving file names"); - opts.optflag("q", "quiet", "never output headers giving file names"); - opts.optflag("", "silent", "synonym of --quiet"); - - let given_options = match opts.parse(&args) { - Ok(m) => m, - Err(_) => { - println!("{}", opts.usage("")); - return 1; - } - }; - - if given_options.opt_present("h") { - println!("{}", opts.usage("")); - return 0; - } - if given_options.opt_present("V") { - version(); - return 0; - } - - settings.follow = given_options.opt_present("f"); + settings.follow = matches.is_present(OPT_FOLLOW); if settings.follow { - if let Some(n) = given_options.opt_str("s") { + if let Some(n) = matches.value_of(OPT_SLEEP_INT) { let parsed: Option = n.parse().ok(); if let Some(m) = parsed { settings.sleep_msec = m * 1000 @@ -120,7 +145,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } - if let Some(pid_str) = given_options.opt_str("pid") { + if let Some(pid_str) = matches.value_of(OPT_PID) { if let Ok(pid) = pid_str.parse() { settings.pid = pid; if pid != 0 { @@ -136,7 +161,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } - match given_options.opt_str("n") { + match matches.value_of(OPT_LINES) { Some(n) => { let mut slice: &str = n.as_ref(); if slice.chars().next().unwrap_or('_') == '+' { @@ -152,7 +177,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } None => { - if let Some(n) = given_options.opt_str("c") { + if let Some(n) = matches.value_of(OPT_BYTES) { let mut slice: &str = n.as_ref(); if slice.chars().next().unwrap_or('_') == '+' { settings.beginning = true; @@ -169,16 +194,19 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } }; - if given_options.opt_present("z") { + if matches.is_present(OPT_ZERO_TERM) { if let FilterMode::Lines(count, _) = settings.mode { settings.mode = FilterMode::Lines(count, 0); } } - let verbose = given_options.opt_present("v"); - let quiet = given_options.opt_present("q") || given_options.opt_present("silent"); + let verbose = matches.is_present(OPT_VERBOSE); + let quiet = matches.is_present(OPT_QUIET) || matches.is_present(OPT_SILENT); - let files = given_options.free; + let files: Vec = matches + .values_of(ARG_FILES) + .map(|v| v.map(ToString::to_string).collect()) + .unwrap_or_default(); if files.is_empty() { let mut buffer = BufReader::new(stdin()); @@ -313,42 +341,6 @@ pub fn parse_size(mut size_slice: &str) -> Result { } } -// It searches for an option in the form of -123123 -// -// In case is found, the options vector will get rid of that object so that -// getopts works correctly. -fn obsolete(options: &[String]) -> (Vec, Option) { - let mut options: Vec = options.to_vec(); - let mut a = 0; - let b = options.len(); - - while a < b { - let current = options[a].clone(); - let current = current.as_bytes(); - - if current.len() > 1 && current[0] == b'-' { - let len = current.len(); - for pos in 1..len { - // Ensure that the argument is only made out of digits - if !(current[pos] as char).is_numeric() { - break; - } - - // If this is the last number - if pos == len - 1 { - options.remove(a); - let number: Option = from_utf8(¤t[1..len]).unwrap().parse().ok(); - return (options, Some(number.unwrap())); - } - } - } - - a += 1; - } - - (options, None) -} - /// When reading files in reverse in `bounded_tail`, this is the size of each /// block read at a time. const BLOCK_SIZE: u64 = 1 << 16; @@ -563,7 +555,3 @@ fn print_byte(stdout: &mut T, ch: u8) { fn print_string(_: &mut T, s: &str) { print!("{}", s); } - -fn version() { - println!("{} {}", NAME, VERSION); -}