@@ -4,15 +4,17 @@ use std::env;
44use std:: path:: Path ;
55
66const HELP_TEXT : & str = "\
7- jsonkdl - convert json to kdl
8- Usage:
9- jsonkdl <input> <output> [options]
7+ Usage: jsonkdl <input> <output> [options]
8+ Converts JSON to KDL.
9+ By default, KDL spec v2 is used.
1010
1111Arguments:
12- <input> Path to input json file
13- <output> Path to output kdl file
12+ <input> Path to input JSON file
13+ <output> Path to output KDL file
1414
15- Aptions:
15+ Options:
16+ -1, --kdl-v1 Convert to KDL v1
17+ -2, --kdl-v2 Convert to KDL v2
1618 -f, --force Overwrite output if it exists
1719 -v, --verbose Print extra information during conversion
1820 -h, --help Show this help message
@@ -23,6 +25,7 @@ pub enum CliError {
2325 MissingInput ,
2426 MissingOutput ,
2527 HelpRequested ,
28+ MultipleKdlVersion ,
2629 InvalidInputPath ( String ) ,
2730 FileExists ( String ) ,
2831 InputNotFound ( String ) ,
@@ -37,6 +40,7 @@ pub struct Args {
3740 pub output : String ,
3841 pub force : bool ,
3942 pub verbose : bool ,
43+ pub kdl_version : i32 ,
4044}
4145
4246impl std:: fmt:: Display for CliError {
@@ -45,6 +49,7 @@ impl std::fmt::Display for CliError {
4549 CliError :: MissingInput => writeln ! ( f, "missing input path" ) ,
4650 CliError :: MissingOutput => writeln ! ( f, "missing output path" ) ,
4751 CliError :: HelpRequested => writeln ! ( f, "help requested" ) ,
52+ CliError :: MultipleKdlVersion => writeln ! ( f, "specify only one of --kdl-v1 or --kdl-v2" ) ,
4853 CliError :: InvalidInputPath ( path) => writeln ! ( f, "not a file: {}" , path) ,
4954 CliError :: FileExists ( path) => writeln ! ( f, "file exists: {} (use --force to overwrite)" , path) ,
5055 CliError :: InputNotFound ( path) => writeln ! ( f, "no such file: {}" , path) ,
@@ -71,33 +76,62 @@ impl From<ConversionError> for CliError {
7176impl Args {
7277 fn parse ( ) -> Result < Self > {
7378 let args: Vec < String > = env:: args ( ) . collect ( ) ;
79+ let mut positional: Vec < String > = vec ! [ ] ;
80+ let mut force = false ;
81+ let mut verbose = false ;
82+ let mut kdl_version = 0 ;
7483
75- // If no args besides the program name, show help
76- if args. len ( ) == 1
77- || args. iter ( ) . any ( |arg| arg == "--help" || arg == "-h" )
78- {
84+ if args. len ( ) == 1 {
7985 return Err ( CliError :: HelpRequested ) ;
8086 }
8187
82- let force = args. iter ( ) . any ( |arg| arg == "--force" || arg == "-f" ) ;
83- let verbose = args. iter ( ) . any ( |arg| arg == "--verbose" || arg == "-v" ) ;
88+ for arg in args. iter ( ) . skip ( 1 ) {
89+ if !arg. starts_with ( '-' ) {
90+ positional. push ( arg. into ( ) ) ;
91+ } else if arg == "-f" || arg == "--force" {
92+ force = true ;
93+ } else if arg == "-v" || arg == "--verbose" {
94+ verbose = true ;
95+ } else if arg == "-1" || arg == "--kdl-v1" {
96+ if kdl_version != 0 {
97+ return Err ( CliError :: MultipleKdlVersion ) ;
98+ }
99+
100+ kdl_version = 1 ;
101+ } else if arg == "-2" || arg == "--kdl-v2" {
102+ if kdl_version != 0 {
103+ return Err ( CliError :: MultipleKdlVersion ) ;
104+ }
105+
106+ kdl_version = 2 ;
107+ } else if arg == "-h" || arg == "--help" {
108+ return Err ( CliError :: HelpRequested ) ;
109+ }
110+ }
111+
112+ if kdl_version == 0 {
113+ kdl_version = 2 ;
114+ } ;
84115
85- let positional: Vec < String > = args
86- . iter ( )
87- . skip ( 1 )
88- . filter ( |arg| !arg. starts_with ( '-' ) )
89- . cloned ( )
90- . collect ( ) ;
116+ let input = positional
117+ . get ( 0 )
118+ . ok_or ( CliError :: MissingInput ) ?
119+ . to_string ( ) ;
91120
92- let input = positional. get ( 0 ) . ok_or ( CliError :: MissingInput ) ?. to_string ( ) ;
93- let output = positional. get ( 1 ) . ok_or ( CliError :: MissingOutput ) ?. to_string ( ) ;
121+ let output = positional
122+ . get ( 1 )
123+ . ok_or ( CliError :: MissingOutput ) ?
124+ . to_string ( ) ;
94125
95- Ok ( Self {
126+ let result = Self {
96127 input,
97128 output,
98129 force,
99130 verbose,
100- } )
131+ kdl_version,
132+ } ;
133+
134+ Ok ( result)
101135 }
102136}
103137
@@ -130,6 +164,6 @@ pub fn run() -> Result<()> {
130164 return Err ( CliError :: FileExists ( args. output ) ) ;
131165 }
132166
133- convert_file_contents ( input_path, output_path, args. verbose ) ?;
167+ convert_file_contents ( input_path, output_path, args. verbose , args . kdl_version ) ?;
134168 Ok ( ( ) )
135169}
0 commit comments