File tree Expand file tree Collapse file tree 1 file changed +22
-2
lines changed
Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change 44// to those terms.
55
66use clap:: { Args , Parser , Subcommand } ;
7- use std:: path:: PathBuf ;
7+ use std:: { ffi :: OsStr , path:: PathBuf } ;
88
99#[ derive( Parser , Debug ) ]
1010#[ command(
@@ -79,7 +79,7 @@ pub struct DrawArgs {
7979 pub file : PathBuf ,
8080
8181 /// Output file
82- #[ arg( short, value_parser = must_not_exist ) ]
82+ #[ arg( short, value_parser = validate_image_output ) ]
8383 pub output : Option < PathBuf > ,
8484}
8585
@@ -117,6 +117,26 @@ fn must_not_exist(s: &str) -> Result<PathBuf, String> {
117117 }
118118}
119119
120+ fn validate_image_output ( s : & str ) -> Result < PathBuf , String > {
121+ let mp = PathBuf :: from ( s) ;
122+
123+ if mp. exists ( ) {
124+ return Err ( format ! ( "{} should not already exist." , mp. display( ) ) ) ;
125+ }
126+ if let Some ( ext) = mp. extension ( ) {
127+ if ext != OsStr :: new ( "png" ) {
128+ return Err ( format ! (
129+ "{} should have png extension (.png)." ,
130+ mp. display( )
131+ ) ) ;
132+ }
133+ } else {
134+ return Err ( "Output file must have an extension" . to_string ( ) ) ;
135+ }
136+
137+ Ok ( mp)
138+ }
139+
120140fn validate_block_width ( val : & str ) -> Result < usize , String > {
121141 match val. parse :: < usize > ( ) {
122142 Ok ( v) => {
You can’t perform that action at this time.
0 commit comments