Use 'clap' for better argument parsing

This commit is contained in:
Skia 2021-09-07 14:14:54 +02:00
parent 7a314d96e5
commit 7653a4cad2
3 changed files with 157 additions and 12 deletions

108
Cargo.lock generated
View file

@ -1,17 +1,75 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.2" version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.7" version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "libc"
version = "0.2.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -35,10 +93,60 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "swaysome" name = "swaysome"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clap",
"serde_json", "serde_json",
] ]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View file

@ -9,3 +9,4 @@ edition = "2018"
[dependencies] [dependencies]
byteorder = "1" byteorder = "1"
serde_json = "1" serde_json = "1"
clap = "2"

View file

@ -1,7 +1,8 @@
// client.rs
extern crate byteorder; extern crate byteorder;
extern crate serde_json; extern crate serde_json;
extern crate clap;
use clap::{Arg, App, SubCommand};
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::io::{Read, Write}; use std::io::{Read, Write};
@ -238,20 +239,55 @@ fn init_workspaces(stream: &UnixStream, workspace_name: &String) {
} }
} }
fn main() { fn main() {
// `args` returns the arguments passed to the program let matches = App::new("swaysome")
let args: Vec<String> = env::args().map(|x| x.to_string()) .version("1.0")
.collect(); .author("Skia <skia@hya.sk>")
.about("Better multimonitor handling for sway")
.subcommand(SubCommand::with_name("init")
.about("Initialize the workspaces for all the outputs")
.arg(Arg::with_name("index")
.help("The index to initialize with")
.required(true)
.takes_value(true)))
.subcommand(SubCommand::with_name("focus")
.about("Focus to another workspace on the same output")
.arg(Arg::with_name("index")
.help("The index to focus on")
.required(true)
.takes_value(true)))
.subcommand(SubCommand::with_name("focus_all_outputs")
.about("Focus to another workspace on all the outputs")
.arg(Arg::with_name("index")
.help("The index to focus on")
.required(true)
.takes_value(true)))
.subcommand(SubCommand::with_name("move")
.about("Move the focused container to another workspace on the same output")
.arg(Arg::with_name("index")
.help("The index to move the container to")
.required(true)
.takes_value(true)))
.subcommand(SubCommand::with_name("next_output")
.about("Move the focused container to the next output"))
.subcommand(SubCommand::with_name("prev_output")
.about("Move the focused container to the previous output"))
.get_matches();
let stream = get_stream(); let stream = get_stream();
match args[1].as_str() { if let Some(matches) = matches.subcommand_matches("init") {
"init" => init_workspaces(&stream, &args[2]), init_workspaces(&stream, &matches.value_of("index").unwrap().to_string());
"move" => move_container_to_workspace(&stream, &args[2]), } else if let Some(matches) = matches.subcommand_matches("move") {
"focus" => focus_to_workspace(&stream, &args[2]), move_container_to_workspace(&stream, &matches.value_of("index").unwrap().to_string());
"focus_all_outputs" => focus_all_outputs_to_workspace(&stream, &args[2]), } else if let Some(matches) = matches.subcommand_matches("focus") {
"next_output" => move_container_to_next_output(&stream), focus_to_workspace(&stream, &matches.value_of("index").unwrap().to_string());
"prev_output" => move_container_to_prev_output(&stream), } else if let Some(matches) = matches.subcommand_matches("focus_all_outputs") {
_ => {}, focus_all_outputs_to_workspace(&stream, &matches.value_of("index").unwrap().to_string());
} else if let Some(_) = matches.subcommand_matches("next_output") {
move_container_to_next_output(&stream);
} else if let Some(_) = matches.subcommand_matches("prev_output") {
move_container_to_prev_output(&stream);
} }
} }