Comments (7)
The orignial motivation of this crate was to handle files, but I think directory support makes.
It has some basic support in ClioPath and the clap validaotor already, e.g. you can get clap to validate that a path is a directory that exists like so:
use clap::Parser;
use clio::ClioPath;
#[derive(Parser)]
#[clap(name = "foo")]
struct Opt {
/// path to directory
#[clap(value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
log_dir: ClioPath,
}
fn main() {
let opt = Opt::parse();
println!("log dir is: {}", opt.log_dir);
}
$: foo
log dir is: "."
$: foo /tmp/
log dir is: "/tmp/"
$: foo /tmp/not-exists
error: Invalid value for <LOG_DIR>: Invalid path "/tmp/not-exists": No such file or directory (os error 2)
$: foo /tmp/file
error: Invalid value for <LOG_DIR>: Invalid path "/tmp/file": Not a directory (os error 20)
What were you imaginging? A specific type so you could just use #[clap(value_parser)]?
from clio.
Nice. Any chance you can provide a sample for how to do that with the "builder" clap pattern?
I have everything working except this bit:
Arg::new("path").short('p').long("path")
.default_value(WORKING_DIR_PATH.as_os_str())
.value_parser(value_parser!(ClioPath))
error
error[E0599]: the method `value_parser` exists for reference `&&&&&&_AutoValueParser<ClioPath>`, but its trait bounds were not satisfied
--> butane_cli/src/main.rs:23:27
|
23 | .value_parser(value_parser!(ClioPath))
| ^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `&&&&&&_AutoValueParser<ClioPath>` due to unsatisfied trait bounds
|
::: /home/jayvdb/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clap-4.1.6/src/builder/value_parser.rs:2187:1
|
2187 | pub struct _AutoValueParser<T>(std::marker::PhantomData<T>);
| ------------------------------ doesn't satisfy `_: _ValueParserViaParse`
It would be useful to have helpers for a "base directory" arg. i.e. encapsulate the lazy_static
needed to get std::env::current_dir()
into a clap default_value
.
from clio.
You have the right code, you just need to enable the clap-parse
feature for clio in your Cargo.toml
. You can add the validation option to the parser returned by value_parser!(ClioPath)
.
Also if you enable the string
fearute on clap, then you can use an OsString as a default value so you don't need a static ref, so no need for lazy static.
That said I am not sure that I see the value in current_dir().unwrap_or(".".into())
as the main value of current_dir() is in the error if the current_dir does not exists/has wrong permissions, as for the default shown in the help I think ./
is more expressive that showing the whole path of the current directory.
I am probably thinking about this too much but it did make me raise #24 as with .
it can both return true
for exists()
but still return not found
when you try to open it. It also made realise that the error reporting when the default is invalid is terrible, so I may raise a clap issue about that.
Here is a full example incase it helps.
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.1", features = ["string"] }
clio = { version = "0.3.2", features = ["clap-parse"] }
use clap::{Parser, value_parser, Arg};
use clio::ClioPath;
use std::path::PathBuf;
pub fn working_dir_path() -> PathBuf {
match std::env::current_dir() {
Ok(path) => path,
Err(_) => PathBuf::from("."),
}
}
fn main() {
let app = clap::Command::new("hello")
.version(env!("CARGO_PKG_VERSION"))
.arg(
Arg::new("path").short('p').long("path")
.default_value(working_dir_path().into_os_string())
.value_parser(value_parser!(ClioPath).exists().is_dir())
);
let args = app.get_matches();
let log_dir = args.get_one::<ClioPath>("path").unwrap().clone();
println!("log dir is: {}", log_dir);
}
from clio.
Turns out there is already clap-rs/clap#4643 "assert_defaults assumes parsers are stateless"
from clio.
The other issue that may cause that error is that cargo may be including both clap 3 and clap 4 and clio then implements the clap 3 _AutoValueParser but clap is expecting it to implement the clap 4 _AutoValueParser, in that case I resort to updtaing the Cargo.lock file manullaly.
from clio.
Adding feature clap-parse
did the trick.
Sooo.. I suggest updating the readme to indicate that it does this directory stuff.
There are lots of little gotchas like #24 which make this problem more complex that it seems, which is why a helper is warranted.
from clio.
Fixed #24 and added a directory example to the docs.
from clio.
Related Issues (17)
- Doesn't play nicely with clap 3.1's derive API HOT 2
- Warnings from clap 3.2 HOT 5
- Compatibility with Clap 4? HOT 4
- Feature-gated functionality not marked in docs HOT 1
- Errors displayed poorly HOT 1
- Clap 4.1.0 compat HOT 2
- FYI — directories / `clio_extended` HOT 6
- Atomic write support? HOT 1
- More configuration options on `OsStrParser` (maybe making it a "PathParser`?) HOT 1
- does not compile on rust 1.65.0
- Make seek work with HTTP range requests
- Make seek work with stdin/stdout
- remove Deref<Path> from ClioPath
- Fix length for block devices
- exists() on "." always returns true even if the current working directory has been deleted.
- Github tags HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clio.