@@ -459,6 +459,26 @@ fn page_width() -> usize {
459459 detected. map_or ( MAX_WIDTH , |w| w. min ( MAX_WIDTH ) )
460460}
461461
462+ /// Whether argv asks for help, read before clap parses it: `-h` or `--help`
463+ /// anywhere before `--`, or `help` as the first word that is not an option.
464+ /// The one kind of run allowed to ask the terminal for its background (see
465+ /// `output::theme::allow_background_query`). A miss costs only the fallback
466+ /// palette; a false match on a command that prompts would cost the answer
467+ /// typed ahead, which is why `help` counts only as the subcommand.
468+ pub fn help_requested ( argv : & [ std:: ffi:: OsString ] ) -> bool {
469+ let mut first_word = true ;
470+ for arg in argv. iter ( ) . skip ( 1 ) . filter_map ( |arg| arg. to_str ( ) ) {
471+ match arg {
472+ "--" => return false ,
473+ "-h" | "--help" => return true ,
474+ "help" if first_word => return true ,
475+ _ if !arg. starts_with ( '-' ) => first_word = false ,
476+ _ => { }
477+ }
478+ }
479+ false
480+ }
481+
462482/// The copy of the tree that parses, and so renders every subcommand's
463483/// help: compact, and about the command itself.
464484///
@@ -663,13 +683,15 @@ mod tests {
663683 assert ! ( missing. is_empty( ) , "{}" , missing. join( "\n " ) ) ;
664684 }
665685
666- /// Help and clap's errors use bold and the palette's fixed colors, never
686+ /// Help and clap's errors use bold and the palettes' fixed colors, never
667687 /// one of the sixteen a terminal theme decides — and without 24-bit
668688 /// color, no color at all. See `output::theme`.
669689 #[ test]
670690 fn theme_safe_colors_only ( ) {
671- for truecolor in [ true , false ] {
672- let app = app ( ) . styles ( crate :: output:: theme:: styles_for ( truecolor) ) ;
691+ use crate :: output:: theme:: { styles_in, Look , LOOKS } ;
692+ for & look in LOOKS {
693+ let truecolor = look != Look :: Bold ;
694+ let app = app ( ) . styles ( styles_in ( look) ) ;
673695 let parsing = for_parsing ( apply ( app) ) ;
674696 for argv in [
675697 & [ "mapbox" , "--help" ] [ ..] ,
@@ -687,18 +709,18 @@ mod tests {
687709 let problems = unsafe_styles ( & text, truecolor) ;
688710 assert ! (
689711 problems. is_empty( ) ,
690- "{argv:?} (truecolor: {truecolor }) uses {problems:?}:\n {text}"
712+ "{argv:?} ({look:? }) uses {problems:?}:\n {text}"
691713 ) ;
692714 }
693715 }
694716 }
695717
696718 /// The SGR parameters in `text` that are neither a reset, bold, nor —
697- /// with `truecolor` — a 24-bit color from the palette .
719+ /// with `truecolor` — a 24-bit color from one of the palettes .
698720 fn unsafe_styles ( text : & str , truecolor : bool ) -> Vec < String > {
699- let palette: Vec < String > = crate :: output:: theme:: PALETTE
721+ let palette: Vec < String > = crate :: output:: theme:: colors ( )
700722 . iter ( )
701- . map ( |( c , _ ) | format ! ( "{};{};{}" , c. 0 , c. 1 , c. 2 ) )
723+ . map ( |c | format ! ( "{};{};{}" , c. 0 , c. 1 , c. 2 ) )
702724 . collect ( ) ;
703725 let mut problems = Vec :: new ( ) ;
704726 for sequence in text. split ( "\x1b [" ) . skip ( 1 ) {
@@ -734,6 +756,26 @@ mod tests {
734756 problems
735757 }
736758
759+ #[ test]
760+ fn help_is_recognized_only_where_it_asks_for_help ( ) {
761+ let asks = |args : & [ & str ] | {
762+ let argv: Vec < std:: ffi:: OsString > = std:: iter:: once ( "mapbox" )
763+ . chain ( args. iter ( ) . copied ( ) )
764+ . map ( Into :: into)
765+ . collect ( ) ;
766+ help_requested ( & argv)
767+ } ;
768+ assert ! ( asks( & [ "--help" ] ) ) ;
769+ assert ! ( asks( & [ "styles" , "list" , "-h" ] ) ) ;
770+ assert ! ( asks( & [ "help" , "styles" ] ) ) ;
771+ assert ! ( asks( & [ "--no-color" , "help" ] ) ) ;
772+ // A style named "help" is a value, not a request: deleting it
773+ // prompts, and the prompt must keep its answer.
774+ assert ! ( !asks( & [ "styles" , "delete" , "help" ] ) ) ;
775+ assert ! ( !asks( & [ "tilesets-cli" , "--" , "--help" ] ) ) ;
776+ assert ! ( !asks( & [ "styles" , "list" ] ) ) ;
777+ }
778+
737779 fn shown ( text : & str ) -> Vec < String > {
738780 pieces ( text)
739781 . iter ( )
0 commit comments