@@ -15,7 +15,7 @@ use clap::{Parser, Subcommand};
1515use std:: { path:: PathBuf , process:: ExitCode } ;
1616
1717use completions:: { CompletionsArg , run_shell_completion} ;
18- use config:: ProjectConfig ;
18+ use config:: { CustomLanguagePolicy , ProjectConfig } ;
1919use lsp:: { LspArg , run_language_server} ;
2020use new:: { NewArg , run_create_new} ;
2121use outline:: { OutlineArg , run_outline} ;
@@ -46,9 +46,9 @@ struct App {
4646 /// Path to ast-grep root config, default is sgconfig.yml.
4747 #[ clap( short, long, global = true , value_name = "CONFIG_FILE" ) ]
4848 config : Option < PathBuf > ,
49- /// Allow sgconfig.yml to load native custom language libraries.
50- #[ clap( long, global = true ) ]
51- allow_custom_languages : bool ,
49+ /// Control how sgconfig.yml handles native custom language libraries.
50+ #[ clap( long, global = true , value_enum , default_value_t ) ]
51+ custom_languages : CustomLanguagePolicy ,
5252}
5353
5454#[ derive( Subcommand ) ]
@@ -106,7 +106,7 @@ fn insert_default_run(args: &mut Vec<String>) {
106106/// finding project and setup custom language configuration
107107fn setup_project_is_possible (
108108 args : & [ String ] ,
109- allow_custom_languages : bool ,
109+ custom_languages : CustomLanguagePolicy ,
110110) -> Result < Result < ProjectConfig > > {
111111 let mut config = None ;
112112 for i in 0 ..args. len ( ) {
@@ -127,21 +127,48 @@ fn setup_project_is_possible(
127127 let config_file = ( & args[ i + 1 ] ) . into ( ) ;
128128 config = Some ( config_file) ;
129129 }
130- ProjectConfig :: setup ( config, allow_custom_languages)
130+ ProjectConfig :: setup ( config, custom_languages)
131+ }
132+
133+ fn parse_custom_language_policy ( args : & [ String ] ) -> CustomLanguagePolicy {
134+ let mut policy = None ;
135+ let mut i = 1 ;
136+ while i < args. len ( ) {
137+ let arg = args[ i] . as_str ( ) ;
138+ if arg == "--" {
139+ break ;
140+ }
141+ let value = if arg == "--custom-languages" {
142+ i += 1 ;
143+ args. get ( i) . map ( String :: as_str)
144+ } else {
145+ arg. strip_prefix ( "--custom-languages=" )
146+ } ;
147+ if let Some ( value) = value {
148+ if policy. is_some ( ) {
149+ return CustomLanguagePolicy :: Ignore ;
150+ }
151+ policy = match value {
152+ "allow" => Some ( CustomLanguagePolicy :: Allow ) ,
153+ "ignore" => Some ( CustomLanguagePolicy :: Ignore ) ,
154+ _ => return CustomLanguagePolicy :: Ignore ,
155+ } ;
156+ }
157+ i += 1 ;
158+ }
159+ policy. unwrap_or_default ( )
131160}
132161
133162// this wrapper function is for testing
134163pub fn main_with_args ( args : impl Iterator < Item = String > ) -> Result < ExitCode > {
135164 let mut args: Vec < _ > = args. collect ( ) ;
136165 insert_default_run ( & mut args) ;
137- let allow_custom_languages = args
138- . iter ( )
139- . take_while ( |arg| arg. as_str ( ) != "--" )
140- . any ( |arg| arg == "--allow-custom-languages" ) ;
166+ let custom_languages = parse_custom_language_policy ( & args) ;
141167 // do not unwrap project before cmd parsing
142168 // sg help does not need a valid sgconfig.yml
143- let project = setup_project_is_possible ( & args, allow_custom_languages ) ;
169+ let project = setup_project_is_possible ( & args, custom_languages ) ;
144170 let app = App :: try_parse_from ( args) ?;
171+ debug_assert_eq ! ( custom_languages, app. custom_languages) ;
145172 let project = project?; // unwrap here to report invalid project
146173 match app. command {
147174 Commands :: Run ( arg) => run_with_pattern ( arg, project) ,
@@ -203,6 +230,29 @@ mod test_cli {
203230 insert_default_run ( & mut args) ;
204231 assert_eq ! ( args[ 1 ] , "run" ) ;
205232 }
233+
234+ #[ test]
235+ fn test_custom_language_policy ( ) {
236+ let args = |args : & [ & str ] | {
237+ args
238+ . iter ( )
239+ . map ( |arg| ( * arg) . to_string ( ) )
240+ . collect :: < Vec < _ > > ( )
241+ } ;
242+ assert_eq ! (
243+ parse_custom_language_policy( & args( & [ "sg" , "scan" ] ) ) ,
244+ CustomLanguagePolicy :: Ignore
245+ ) ;
246+ assert_eq ! (
247+ parse_custom_language_policy( & args( & [ "sg" , "scan" , "--custom-languages=allow" ] ) ) ,
248+ CustomLanguagePolicy :: Allow
249+ ) ;
250+ assert_eq ! (
251+ parse_custom_language_policy( & args( & [ "sg" , "run" , "--" , "--custom-languages" , "allow" ] ) ) ,
252+ CustomLanguagePolicy :: Ignore
253+ ) ;
254+ }
255+
206256 #[ test]
207257 fn test_no_arg_run ( ) {
208258 let ret = main_with_args ( [ "sg" . to_owned ( ) ] . into_iter ( ) ) ;
@@ -269,6 +319,10 @@ mod test_cli {
269319 #[ test]
270320 fn test_scan ( ) {
271321 ok ( "scan" ) ;
322+ ok ( "scan --custom-languages allow" ) ;
323+ ok ( "scan --custom-languages ignore" ) ;
324+ error ( "scan --custom-languages deny" ) ;
325+ error ( "scan --custom-languages invalid" ) ;
272326 ok ( "scan dir" ) ;
273327 ok ( "scan -r test-rule.yml dir" ) ;
274328 ok ( "scan -c test-rule.yml dir" ) ;
0 commit comments