2020-07-16 21:48:00 +00:00
// This feature allows users of the app to pass subcommands in the fashion of short or long flags.
// You may be familiar with it if you ever used [`pacman`](https://wiki.archlinux.org/index.php/pacman).
// Some made up examples of what flag subcommands are:
//
// ```shell
// $ pacman -S
// ^--- short flag subcommand.
// $ pacman --sync
// ^--- long flag subcommand.
2020-06-13 16:28:10 +00:00
// $ pacman -Ss
2020-07-16 21:48:00 +00:00
// ^--- short flag subcommand followed by a short flag
// (users can "stack" short subcommands with short flags or with other short flag subcommands)
// $ pacman -S -s
// ^--- same as above
// $ pacman -S --sync
// ^--- short flag subcommand followed by a long flag
// ```
// NOTE: Keep in mind that subcommands, flags, and long flags are *case sensitive*: `-Q` and `-q` are different flags/subcommands. For example, you can have both `-Q` subcommand and `-q` flag, and they will be properly disambiguated.
// Let's make a quick program to illustrate.
2020-06-16 01:52:36 +00:00
use clap ::{ App , AppSettings , Arg } ;
2020-06-13 16:28:10 +00:00
fn main ( ) {
let matches = App ::new ( " pacman " )
. about ( " package manager utility " )
. version ( " 5.2.1 " )
. setting ( AppSettings ::SubcommandRequiredElseHelp )
. author ( " Pacman Development Team " )
// Query subcommand
//
// Only a few of its arguments are implemented below.
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " query " )
. short_flag ( 'Q' )
. long_flag ( " query " )
2020-06-13 16:28:10 +00:00
. about ( " Query the package database. " )
. arg (
Arg ::new ( " search " )
. short ( 's' )
. long ( " search " )
. about ( " search locally-installed packages for matching strings " )
2020-07-08 01:26:38 +00:00
. conflicts_with ( " info " )
. multiple_values ( true ) ,
2020-06-13 16:28:10 +00:00
)
. arg (
Arg ::new ( " info " )
. long ( " info " )
. short ( 'i' )
2020-07-08 01:26:38 +00:00
. conflicts_with ( " search " )
. about ( " view package information " )
. multiple_values ( true ) ,
2020-06-13 16:28:10 +00:00
) ,
)
// Sync subcommand
//
// Only a few of its arguments are implemented below.
. subcommand (
2020-06-16 01:52:36 +00:00
App ::new ( " sync " )
. short_flag ( 'S' )
. long_flag ( " sync " )
2020-06-13 16:28:10 +00:00
. about ( " Synchronize packages. " )
. arg (
Arg ::new ( " search " )
. short ( 's' )
. long ( " search " )
2020-07-08 01:26:38 +00:00
. conflicts_with ( " info " )
. takes_value ( true )
. multiple_values ( true )
. about ( " search remote repositories for matching strings " ) ,
2020-06-13 16:28:10 +00:00
)
. arg (
Arg ::new ( " info " )
. long ( " info " )
2020-07-08 01:26:38 +00:00
. conflicts_with ( " search " )
2020-06-13 16:28:10 +00:00
. short ( 'i' )
2020-07-08 01:26:38 +00:00
. about ( " view package information " ) ,
2020-06-13 16:28:10 +00:00
)
. arg (
Arg ::new ( " package " )
2020-07-08 01:26:38 +00:00
. about ( " packages " )
2020-06-13 16:28:10 +00:00
. multiple ( true )
2020-07-08 01:26:38 +00:00
. required_unless_one ( & [ " search " ] )
2020-06-13 16:28:10 +00:00
. takes_value ( true ) ,
) ,
)
. get_matches ( ) ;
match matches . subcommand ( ) {
( " sync " , Some ( sync_matches ) ) = > {
2020-07-08 01:26:38 +00:00
if sync_matches . is_present ( " search " ) {
let packages : Vec < _ > = sync_matches . values_of ( " search " ) . unwrap ( ) . collect ( ) ;
let values = packages . join ( " , " ) ;
println! ( " Searching for {} ... " , values ) ;
return ;
}
let packages : Vec < _ > = sync_matches . values_of ( " package " ) . unwrap ( ) . collect ( ) ;
let values = packages . join ( " , " ) ;
2020-06-13 16:28:10 +00:00
if sync_matches . is_present ( " info " ) {
2020-07-08 01:26:38 +00:00
println! ( " Retrieving info for {} ... " , values ) ;
2020-06-13 16:28:10 +00:00
} else {
2020-07-08 01:26:38 +00:00
println! ( " Installing {} ... " , values ) ;
2020-06-13 16:28:10 +00:00
}
}
( " query " , Some ( query_matches ) ) = > {
2020-07-08 01:26:38 +00:00
if let Some ( packages ) = query_matches . values_of ( " info " ) {
let comma_sep = packages . collect ::< Vec < _ > > ( ) . join ( " , " ) ;
2020-06-17 00:01:19 +00:00
println! ( " Retrieving info for {} ... " , comma_sep ) ;
2020-07-08 01:26:38 +00:00
} else if let Some ( queries ) = query_matches . values_of ( " search " ) {
let comma_sep = queries . collect ::< Vec < _ > > ( ) . join ( " , " ) ;
2020-06-17 00:01:19 +00:00
println! ( " Searching Locally for {} ... " , comma_sep ) ;
2020-06-13 16:28:10 +00:00
} else {
println! ( " Displaying all locally installed packages... " ) ;
}
}
_ = > unreachable! ( ) , // If all subcommands are defined above, anything else is unreachable
}
}