Repository: erayerdin/tgcli (language: python)

Issue:
Update to Clap 4
Update to Clap 4 and fix accordingly.

Also, the `option` flag for `bot send poll` had to be changed.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a311163..2575f71 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to
 [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [v0.6.1-alpha.1] - 2023-03-02
+
+### Fixed
+
+ - Update `clap` to `4.1.6`
+
+### Changed
+
+ - Uniformed the `option` argument of `bot send poll`: instead of `-o foo -o bar`, now it is `-o foo bar`
+
 ## [v0.5.1-alpha.1] - 2023-01-21
 
 ### Changed
diff --git a/Cargo.lock b/Cargo.lock
index c3a267e..0ae7281 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -22,7 +22,7 @@ version = "0.2.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
 dependencies = [
- "hermit-abi",
+ "hermit-abi 0.1.18",
  "libc",
  "winapi",
 ]
@@ -82,26 +82,34 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
 [[package]]
 name = "clap"
-version = "3.2.23"
+version = "4.1.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
+checksum = "c3d7ae14b20b94cb02149ed21a86c423859cbe18dc7ed69845cace50e52b40a5"
 dependencies = [
- "atty",
  "bitflags",
  "clap_derive",
  "clap_lex",
- "indexmap",
+ "is-terminal",
  "once_cell",
  "strsim",
  "termcolor",
- "textwrap",
+]
+
+[[package]]
+name = "clap-verbosity-flag"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23e2b6c3dcdb73299f48ae05b294da14e2f560b3ed2c09e742269eb1b22af231"
+dependencies = [
+ "clap",
+ "log",
 ]
 
 [[package]]
 name = "clap_derive"
-version = "3.2.18"
+version = "4.1.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
+checksum = "44bec8e5c9d09e439c4335b1af0abaab56dcf3b94999a936e1bb47b9134288f0"
 dependencies = [
  "heck",
  "proc-macro-error",
@@ -112,9 +120,9 @@ dependencies = [
 
 [[package]]
 name = "clap_lex"
-version = "0.2.4"
+version = "0.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
+checksum = "350b9cf31731f9957399229e9b2adc51eeabdfbe9d71d9a0552275fd12710d09"
 dependencies = [
  "os_str_bytes",
 ]
@@ -179,6 +187,27 @@ dependencies = [
  "cfg-if",
 ]
 
+[[package]]
+name = "errno"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
+dependencies = [
+ "errno-dragonfly",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "errno-dragonfly"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
+dependencies = [
+ "cc",
+ "libc",
+]
+
 [[package]]
 name = "fern"
 version = "0.6.0"
@@ -366,6 +395,12 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "hermit-abi"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
+
 [[package]]
 name = "http"
 version = "0.2.5"
@@ -458,12 +493,34 @@ dependencies = [
  "hashbrown",
 ]
 
+[[package]]
+name = "io-lifetimes"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1abeb7a0dd0f8181267ff8adc397075586500b81b28a73e8a0208b00fc170fb3"
+dependencies = [
+ "libc",
+ "windows-sys",
+]
+
 [[package]]
 name = "ipnet"
 version = "2.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9"
 
+[[package]]
+name = "is-terminal"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "21b6b32576413a8e69b90e952e4a026476040d81017b80445deda5f2d3921857"
+dependencies = [
+ "hermit-abi 0.3.1",
+ "io-lifetimes",
+ "rustix",
+ "windows-sys",
+]
+
 [[package]]
 name = "itertools"
 version = "0.10.1"
@@ -500,6 +557,12 @@ version = "0.2.139"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
 
+[[package]]
+name = "linux-raw-sys"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
+
 [[package]]
 name = "log"
 version = "0.4.14"
@@ -592,7 +655,7 @@ version = "1.13.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
 dependencies = [
- "hermit-abi",
+ "hermit-abi 0.1.18",
  "libc",
 ]
 
@@ -875,6 +938,20 @@ dependencies = [
  "semver",
 ]
 
+[[package]]
+name = "rustix"
+version = "0.36.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644"
+dependencies = [
+ "bitflags",
+ "errno",
+ "io-lifetimes",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys",
+]
+
 [[package]]
 name = "ryu"
 version = "1.0.5"
@@ -1025,18 +1102,13 @@ version = "0.2.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "13a4ec180a2de59b57434704ccfad967f789b12737738798fa08798cd5824c16"
 
-[[package]]
-name = "textwrap"
-version = "0.16.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
-
 [[package]]
 name = "tgcli"
-version = "0.5.1-alpha.1"
+version = "0.6.0-alpha.1"
 dependencies = [
  "assert_cmd",
  "clap",
+ "clap-verbosity-flag",
  "dotenv",
  "fern",
  "log",
@@ -1322,6 +1394,72 @@ version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 
+[[package]]
+name = "windows-sys"
+version = "0.45.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
+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.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.42.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
+
 [[package]]
 name = "winreg"
 version = "0.7.0"
diff --git a/Cargo.toml b/Cargo.toml
index a2f2639..a778845 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "tgcli"
-version = "0.5.1-alpha.1"
+version = "0.6.0-alpha.1"
 authors = ["Eray Erdin <eraygezer.94@gmail.com>"]
 edition = "2021"
 description = "A Telegram Terminal Application"
@@ -12,9 +12,10 @@ mime = "0.3.16"
 mime_guess = "2.0.3"
 url = "2.2.2"
 serde_json = "1.0.71"
+clap-verbosity-flag = "2.0.0"
 
 [dependencies.clap]
-version = "3.2.23"
+version = "4.1.6"
 features = ["derive", "env"]
 
 [dependencies.fern]
diff --git a/docs/bot.md b/docs/bot.md
index 530f625..c21c2d2 100644
--- a/docs/bot.md
+++ b/docs/bot.md
@@ -223,19 +223,19 @@ tgcli bot send poll --help
 | Short Flag | Full Flag | Required/Optional | Description                                                |
 | ---------- | --------- | ----------------- | ---------------------------------------------------------- |
 |            | question  | Required          | The question for poll.                                     |
-| -o         | --option  | Required[^2]      | A single option for poll. You can define multiple options. |
+| -o         | --option  | Required[^2]      | A single option for poll. |
 
 To start a poll:
 
 ```bash
 # a plain poll
-tgcli bot send --receiver 1234 poll "Am I a ghost?" -o "Yes" -o "No"
+tgcli bot send --receiver 1234 poll "Am I a ghost?" -o "Yes" "No"
 ```
 
 !!! note
     Also keep in mind the the order of `-o`/`--option` is preserved for polls.
 
-[^2]: You need to define at least two options for a valid poll.
+[^2]: You need to define at least two options and at most 10 options for a valid poll. See [Telegram documentation](https://core.telegram.org/bots/api#sendpoll) for its limitations.
 
 ### location
 
diff --git a/nfpm.yaml b/nfpm.yaml
index 8a82c8b..996a204 100644
--- a/nfpm.yaml
+++ b/nfpm.yaml
@@ -5,7 +5,7 @@
 name: "tgcli"
 arch: "amd64"
 platform: "linux"
-version: "v0.5.0-alpha.6"
+version: "v0.6.0-alpha.1"
 section: "default"
 priority: "extra"
 provides:
diff --git a/src/cli/logging.rs b/src/cli/logging.rs
index 423da99..a0efa8d 100644
--- a/src/cli/logging.rs
+++ b/src/cli/logging.rs
@@ -13,8 +13,17 @@ pub(crate) fn set_logger(
     // 2 - Debug, Info, Warn, Error + Self Target + Level Labels + Location Labels
     // 3 - Trace, Debug, Info, Warn, Error + Self Target + Level Labels + Location Labels
     // 4 - Trace, Debug, Info, Warn, Error + All Targets + Level Labels + Location Labels
-    verbosity: u8,
+    verbosity_level: &clap_verbosity_flag::Verbosity,
 ) -> Result<(), log::SetLoggerError> {
+    let verbosity: u8 = match verbosity_level.log_level_filter() {
+        log::LevelFilter::Off => 0,
+        log::LevelFilter::Trace => 1,
+        log::LevelFilter::Debug => 2,
+        log::LevelFilter::Info => 3,
+        log::LevelFilter::Warn => 4,
+        log::LevelFilter::Error => 5,
+    };
+
     let colors = ColoredLevelConfig::new()
         .error(Color::BrightRed)
         .warn(Color::Yellow)
@@ -36,52 +45,29 @@ pub(crate) fn set_logger(
                     metadata.level() != log::LevelFilter::Error
                         || metadata.level() != log::LevelFilter::Warn
                 })
-                .format(move |out, message, record| {
-                    // if cfg!(debug_assertions) {
-                    //     out.finish(format_args!(
-                    //         "[{}][{}] {}",
-                    //         record.level(),
-                    //         record.target(),
-                    //         message
-                    //     ))
-                    // } else {
-                    //     match record.level() {
-                    //         log::Level::Error => out.finish(format_args!("Error: {}", message)),
-                    //         log::Level::Warn => out.finish(format_args!("Warning: {}", message)),
-                    //         _ => out.finish(format_args!("{}", message)),
-                    //     }
-                    // }
-                    match verbosity {
-                        1 => out.finish(format_args!(
-                            "[{levelname}] {message}",
-                            levelname = record.level(),
-                            message = message,
-                        )),
-                        2..=u8::MAX => out.finish(format_args!(
-                            "[{levelname}][{targetname}] {message}",
-                            levelname = record.level(),
-                            targetname = record.target(),
-                            message = message,
-                        )),
-                        0 => out.finish(format_args!(
-                            "\x1B[{}m{}\x1B[0m",
-                            colors.get_color(&record.level()).to_fg_str(),
-                            message
-                        )),
-                    }
+                .format(move |out, message, record| match verbosity {
+                    1 => out.finish(format_args!(
+                        "[{levelname}] {message}",
+                        levelname = record.level(),
+                        message = message,
+                    )),
+                    2..=u8::MAX => out.finish(format_args!(
+                        "[{levelname}][{targetname}] {message}",
+                        levelname = record.level(),
+                        targetname = record.target(),
+                        message = message,
+                    )),
+                    0 => out.finish(format_args!(
+                        "\x1B[{}m{}\x1B[0m",
+                        colors.get_color(&record.level()).to_fg_str(),
+                        message
+                    )),
+                })
+                .level(match verbosity {
+                    0 => log::LevelFilter::Info,
+                    1 | 2 => log::LevelFilter::Debug,
+                    _ => log::LevelFilter::Trace,
                 })
-                .level(
-                    // if cfg!(debug_assertions) {
-                    //     log::LevelFilter::Trace
-                    // } else {
-                    //     log::LevelFilter::Info
-                    // }
-                    match verbosity {
-                        0 => log::LevelFilter::Info,
-                        1 | 2 => log::LevelFilter::Debug,
-                        _ => log::LevelFilter::Trace,
-                    },
-                )
                 .chain(std::io::stdout()),
         )
         // stderr chain
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 23ea8b0..f3eab13 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -38,8 +38,8 @@ pub(crate) mod validators;
 #[derive(Parser)]
 pub struct Cli {
     /// How much verbose the output should be.
-    #[clap(short, long = "verbose", max_occurrences = 4, parse(from_occurrences))]
-    verbosity: u8,
+    #[clap(flatten)]
+    verbose: clap_verbosity_flag::Verbosity,
     #[clap(subcommand)]
     subcommands: CliSubcommands,
 }
@@ -64,7 +64,7 @@ enum BotSubcommands {
         #[clap(short, long)]
         receiver: String,
         /// The format of message.
-        #[clap(long, arg_enum, default_value = "markdown")]
+        #[clap(long, value_enum, default_value = "markdown")]
         format: MessageFormat,
         /// Whether to send notification to user.
         #[clap(long)]
@@ -84,40 +84,40 @@ pub(crate) enum SendSubcommands {
     /// A file.
     Document {
         /// The file to be sent.
-        #[clap(validator = file_presence_validator)]
+        #[clap(value_parser = file_presence_validator)]
         file: path::PathBuf,
         /// The file caption.
-        #[clap(short, long, validator = caption_validator)]
+        #[clap(short, long, value_parser = caption_validator)]
         message: Option<String>,
         /// A preview image for file.
-        #[clap(long, validator = file_presence_validator)]
+        #[clap(long, value_parser = file_presence_validator)]
         thumbnail: Option<path::PathBuf>,
     },
     /// A message with image viewer.
     Photo {
         /// The file to be sent.
-        #[clap(validator = image_validator)]
+        #[clap(value_parser = image_validator)]
         file: path::PathBuf,
         /// The file caption.
-        #[clap(short, long, validator = caption_validator)]
+        #[clap(short, long, value_parser = caption_validator)]
         message: Option<String>,
     },
     /// A message with video viewer.
     Video {
         /// The file to be sent.
-        #[clap(validator = video_validator)]
+        #[clap(value_parser = video_validator)]
         file: path::PathBuf,
         /// The file caption.
-        #[clap(short, long, validator = caption_validator)]
+        #[clap(short, long, value_parser = caption_validator)]
         message: Option<String>,
     },
     /// A message with audio player.
     Audio {
         /// The file to be sent.
-        #[clap(validator = audio_validator)]
+        #[clap(value_parser = audio_validator)]
         file: path::PathBuf,
         /// The file caption.
-        #[clap(short, long, validator = caption_validator)]
+        #[clap(short, long, value_parser = caption_validator)]
         message: Option<String>,
         #[clap(long)]
         performer: Option<String>,
@@ -127,10 +127,11 @@ pub(crate) enum SendSubcommands {
     /// A message with question and options.
     Poll {
         /// Question.
-        #[clap(validator = poll_question_validator)]
+        #[clap(value_parser = poll_question_validator)]
         question: String,
         /// One of options.
-        #[clap(short = 'o', long = "option", multiple_occurrences = true, min_values = 2, validator = poll_option_validator)]
+        // max 10, ref: https://core.telegram.org/bots/api#sendpoll
+        #[clap(short = 'o', long = "option", num_args = 2..10, value_parser = poll_option_validator)]
         options: Vec<String>,
     },
     /// A location on earth.
@@ -145,10 +146,10 @@ pub(crate) enum SendSubcommands {
 }
 
 pub fn match_app(cli: Cli) -> Result<(), OperationError> {
-    set_logger(cli.verbosity)
+    set_logger(&cli.verbose)
         .map_err(|set_logger_error| OperationError::LoggerError(set_logger_error))?;
 
-    let root_params = RootParams::new(cli.verbosity);
+    let root_params = RootParams::new(cli.verbose);
 
     match cli.subcommands {
         CliSubcommands::Bot { token, subcommands } => {
diff --git a/src/main.rs b/src/main.rs
index c1f945f..b02ad2a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,7 +6,7 @@
 
 use std::process;
 
-use clap::StructOpt;
+use clap::Parser;
 use log::error;
 use tgcli::cli::{match_app, Cli};
 
diff --git a/src/operations/bot/send/mod.rs b/src/operations/bot/send/mod.rs
index a175a84..da7f0f4 100644
--- a/src/operations/bot/send/mod.rs
+++ b/src/operations/bot/send/mod.rs
@@ -14,7 +14,7 @@ pub(crate) mod photo;
 pub(crate) mod poll;
 pub(crate) mod video;
 
-#[derive(Debug, Clone, ArgEnum)]
+#[derive(Debug, Clone, ValueEnum)]
 pub(crate) enum MessageFormat {
     Markdown,
     HTML,
diff --git a/src/operations/mod.rs b/src/operations/mod.rs
index 97e1fbe..561978a 100644
--- a/src/operations/mod.rs
+++ b/src/operations/mod.rs
@@ -11,11 +11,11 @@ pub(crate) mod bot;
 #[derive(Debug)]
 pub(crate) struct RootParams {
     #[allow(dead_code)]
-    verbosity: u8,
+    verbosity: clap_verbosity_flag::Verbosity,
 }
 
 impl RootParams {
-    pub(crate) fn new(verbosity: u8) -> Self {
+    pub(crate) fn new(verbosity: clap_verbosity_flag::Verbosity) -> Self {
         Self { verbosity }
     }
 }


Test patch:
diff --git a/tests/bot_send_poll.rs b/tests/bot_send_poll.rs
index 111f984..d731d91 100644
--- a/tests/bot_send_poll.rs
+++ b/tests/bot_send_poll.rs
@@ -27,8 +27,7 @@ fn send_poll(mut binary: Command) {
             .expect("TELEGRAM_RECEIVER environment variable could not be found. Please create .env file and define it."),
         "poll",
         "foo",
-        "-o", "bar",
-        "-o", "baz",
+        "-o", "bar", "baz",
     ]).assert();
 
     assertion.success();
@@ -80,8 +79,7 @@ fn send_poll_overused_chars_option(mut binary: Command) {
             .expect("TELEGRAM_RECEIVER environment variable could not be found. Please create .env file and define it."),
         "poll",
         "foo",
-        "-o", &option,
-        "-o", "bar",
+        "-o", &option, "bar",
     ]).assert();
 
     assertion.failure();