11use anyhow:: Context ;
2- use clap:: { ArgAction , Parser , ValueEnum } ;
2+ use clap:: Parser ;
33use codeql_extractor:: trap;
44use figment:: {
55 providers:: { Env , Serialized } ,
66 Figment ,
77} ;
8+ use num_traits:: Zero ;
9+ use rust_extractor_macros:: extractor_cli_config;
810use serde:: { Deserialize , Serialize } ;
11+ use std:: ops:: Not ;
912use std:: path:: PathBuf ;
1013
11- #[ derive( Debug , PartialEq , Eq , Default , Serialize , Deserialize , Clone , Copy , ValueEnum ) ]
14+ #[ derive( Debug , PartialEq , Eq , Default , Serialize , Deserialize , Clone , Copy , clap :: ValueEnum ) ]
1215#[ serde( rename_all = "lowercase" ) ]
1316#[ clap( rename_all = "lowercase" ) ]
1417pub enum Compression {
@@ -26,8 +29,7 @@ impl From<Compression> for trap::Compression {
2629 }
2730}
2831
29- #[ serde_with:: apply( _ => #[ serde( default ) ] ) ]
30- #[ derive( Debug , Deserialize , Default ) ]
32+ #[ extractor_cli_config]
3133pub struct Config {
3234 pub scratch_dir : PathBuf ,
3335 pub trap_dir : PathBuf ,
@@ -38,40 +40,13 @@ pub struct Config {
3840 pub inputs : Vec < PathBuf > ,
3941}
4042
41- #[ serde_with:: apply( _ => #[ serde( skip_serializing_if = "is_default" ) ] ) ]
42- #[ derive( clap:: Parser , Serialize ) ]
43- #[ command( about, long_about = None ) ]
44- struct CliArgs {
45- #[ arg( long) ]
46- scratch_dir : Option < PathBuf > ,
47- #[ arg( long) ]
48- trap_dir : Option < PathBuf > ,
49- #[ arg( long) ]
50- source_archive_dir : Option < PathBuf > ,
51- #[ arg( long) ]
52- compression : Option < Compression > ,
53- #[ arg( short, long, action = ArgAction :: Count ) ]
54- verbose : u8 ,
55- #[ arg( long) ]
56- inputs_file : Option < PathBuf > ,
57-
58- inputs : Vec < PathBuf > ,
59- }
60-
61- fn is_default < T : Default + PartialEq > ( t : & T ) -> bool {
62- * t == Default :: default ( )
63- }
64-
6543impl Config {
6644 pub fn extract ( ) -> anyhow:: Result < Config > {
67- let mut cli_args = CliArgs :: parse ( ) ;
68- if let Some ( inputs_file) = cli_args. inputs_file . take ( ) {
69- let inputs_list = std:: fs:: read_to_string ( inputs_file) . context ( "reading file list" ) ?;
70- cli_args
71- . inputs
72- . extend ( inputs_list. split_terminator ( "\n " ) . map ( PathBuf :: from) ) ;
73- }
45+ let args = argfile:: expand_args ( argfile:: parse_fromfile, argfile:: PREFIX )
46+ . context ( "expanding parameter files" ) ?;
47+ let cli_args = CliConfig :: parse_from ( args) ;
7448 Figment :: new ( )
49+ . merge ( Env :: prefixed ( "CODEQL_" ) )
7550 . merge ( Env :: prefixed ( "CODEQL_EXTRACTOR_RUST_" ) )
7651 . merge ( Env :: prefixed ( "CODEQL_EXTRACTOR_RUST_OPTION_" ) )
7752 . merge ( Serialized :: defaults ( cli_args) )
0 commit comments