1
1
use std:: env;
2
- use seahorse:: { App , Command , Context } ;
2
+ use seahorse:: { App , Command , Context , Flag , FlagType } ;
3
3
use url:: percent_encoding:: percent_decode;
4
+ use url:: { Url , Position } ;
4
5
5
6
fn main ( ) {
6
7
let args: Vec < String > = env:: args ( ) . collect ( ) ;
@@ -20,12 +21,8 @@ fn main() {
20
21
. name ( "d" )
21
22
. usage ( "udrs d {}" )
22
23
. action ( d) ,
23
- )
24
- . command (
25
- Command :: new ( )
26
- . name ( "ud" )
27
- . usage ( "udrs ud {}" )
28
- . action ( ud) ,
24
+ )
25
+ . command ( ud_c ( )
29
26
) ;
30
27
app. run ( args) ;
31
28
}
@@ -40,8 +37,48 @@ fn d(c: &Context) {
40
37
println ! ( "{:?}" , res) ;
41
38
}
42
39
43
- fn ud ( _c : & Context ) {
44
- println ! ( "{}" , percent_decode( _c. args[ 0 ] . as_bytes( ) ) . decode_utf8( ) . unwrap( ) ) ;
40
+ fn ud_a ( c : & Context ) {
41
+ let url = Url :: parse ( & c. args [ 0 ] ) . unwrap ( ) ;
42
+ if c. bool_flag ( "lpath" ) {
43
+ println ! ( "{}" , & url[ Position :: BeforePath ..] ) ;
44
+ } else if c. bool_flag ( "domain" ) {
45
+ println ! ( "{}" , url. domain( ) . unwrap( ) ) ;
46
+ } else if c. bool_flag ( "protocol" ) {
47
+ println ! ( "{}" , url. scheme( ) ) ;
48
+ } else {
49
+ println ! ( "{}" , percent_decode( c. args[ 0 ] . as_bytes( ) ) . decode_utf8( ) . unwrap( ) ) ;
50
+ }
51
+ }
52
+
53
+ fn ud_c ( ) -> Command {
54
+ Command :: new ( )
55
+ . name ( "ud" )
56
+ . usage ( "cli ud [url...]" )
57
+ . action ( ud_a)
58
+ . flag (
59
+ Flag :: new (
60
+ "lpath" ,
61
+ "cli ud [url...] --lpath(-l)" ,
62
+ FlagType :: Bool ,
63
+ )
64
+ . alias ( "l" ) ,
65
+ )
66
+ . flag (
67
+ Flag :: new (
68
+ "domain" ,
69
+ "cli ud [url...] --domain(-d)" ,
70
+ FlagType :: Bool ,
71
+ )
72
+ . alias ( "d" ) ,
73
+ )
74
+ . flag (
75
+ Flag :: new (
76
+ "protocol" ,
77
+ "cli ud [url...] --protocol(-p)" ,
78
+ FlagType :: Bool ,
79
+ )
80
+ . alias ( "p" ) ,
81
+ )
45
82
}
46
83
47
84
#[ cfg( test) ]
0 commit comments