clap is weird

This commit is contained in:
2024-11-20 23:18:51 +01:00
parent e45dbad2b0
commit 885a3e1d37
3 changed files with 74 additions and 17 deletions
+8 -3
View File
@@ -2,15 +2,20 @@
export PATH="$PATH:$PWD/target/debug/"
pid=( $(ps -o ppid) )
arg_pid=" --pid ${pid[-2]} "
nav() {
cd "$(navigate ${pid[-2]} $*)"
cd "$(navigate ${arg_pid} $*)"
}
push() {
cd "$(navigate ${pid[-2]} push $*)"
cd "$(navigate push ${arg_pid} $*)"
}
pop() {
cd "$(navigate ${pid[-2]} pop $*)"
cd "$(navigate pop ${arg_pid} $*)"
}
stack() {
"navigate stack ${arg_pid}"
}
+61
View File
@@ -0,0 +1,61 @@
use clap:: {Parser, Args, Subcommand};
/// implements stack for cd wrapper script
#[derive(Debug, Parser)]
#[clap(author, version, about)]
pub struct CommandArgs
{
/// subcommand
#[clap(subcommand)]
action: Action,
/// process id of parent shell
#[arg(long="pid")]
pid: u32,
}
#[derive(Debug, Clone, Subcommand)]
pub enum Action
{
/// navigate to path and add current path to the stack
push(PushArgs),
/// navigate to last entry in stack and remove it
pop(PopArgs),
/// display stack
stack(StackArgs),
// bookmark(BookmarkArgs),
}
#[derive(Debug, Clone, Args)]
pub struct PushArgs
{
/// show stack
show_stack: Option<bool>,
/// change to <path>
path: String,
}
#[derive(Debug, Clone, Args)]
pub struct PopArgs
{
/// show stack
#[arg(short, long)]
show_stack: Option<bool>,
}
#[derive(Debug, Clone, Args)]
pub struct StackArgs
{
/// hide entry numbers
#[arg(short, long)]
hide_numbers: Option<bool>,
/// show n entries
#[arg(short, long="lines")]
lines: Option<u32>,
}
+5 -14
View File
@@ -1,20 +1,11 @@
mod args;
use std::env;
use clap::Parser;
use args::CommandArgs;
fn main()
{
let mut args : Vec<String> = env::args().collect();
let pid : i32 = args[1].parse().unwrap();
// remove arguments which don't require parsing
args.remove(0); // remove command name
args.remove(0); // remove pid
for arg in args
{
if !(arg.clone().chars().nth(0).unwrap() == '-')
{
println!("{}", arg)
}
}
let args = CommandArgs::parse();
}