From 99598a6fa39811dce75a6b90c1ffaf1119a6cffe Mon Sep 17 00:00:00 2001 From: Jumpaku Date: Sat, 3 Aug 2024 23:53:00 +0900 Subject: [PATCH 1/2] fix(python3): fix bug with python3 --- cyamli-docs.html | 4 +- cyamli-docs.md | 2 +- cyamli/cli.g.dart | 922 +++++++++++++++++++++++++++++++ cyamli/cli.gen.go | 18 +- cyamli/cli.yaml | 2 +- cyamli/cli_gen.py | 629 +++++++++++++++++++++ examples/cmd/demo-app/cli.g.dart | 2 +- examples/cmd/demo-app/cli.gen.go | 2 +- examples/cmd/demo-app/cli_gen.py | 15 +- examples/cmd/example/cli.g.dart | 2 +- examples/cmd/example/cli.gen.go | 2 +- examples/cmd/example/cli_gen.py | 35 +- info/version.txt | 2 +- python3/cli_gen.py.tpl | 9 +- schema/cli.schema.json | 2 +- schema/cli.schema.yaml | 2 +- 16 files changed, 1615 insertions(+), 35 deletions(-) create mode 100644 cyamli/cli.g.dart create mode 100644 cyamli/cli_gen.py diff --git a/cyamli-docs.html b/cyamli-docs.html index 81181cf..55fc6ca 100644 --- a/cyamli-docs.html +++ b/cyamli-docs.html @@ -2,13 +2,13 @@ - cyamli (v1.1.5) + cyamli (v1.1.6)

- cyamli (v1.1.5) + cyamli (v1.1.6)

diff --git a/cyamli-docs.md b/cyamli-docs.md index 27be475..c02fd45 100644 --- a/cyamli-docs.md +++ b/cyamli-docs.md @@ -1,4 +1,4 @@ -# cyamli (v1.1.5) +# cyamli (v1.1.6) ## cyamli diff --git a/cyamli/cli.g.dart b/cyamli/cli.g.dart new file mode 100644 index 0000000..7ef40c8 --- /dev/null +++ b/cyamli/cli.g.dart @@ -0,0 +1,922 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND +// generator: github.com/Jumpaku/cyamli v1.1.5 + +// ignore_for_file: unused_local_variable + +typedef Func = Function(List subcommand, Input? input, Exception? inputErr); + + + + + +class CLI { + Func? FUNC; + + final CLI_Generate generate = CLI_Generate(); + + final CLI_List list = CLI_List(); + + final CLI_Validate validate = CLI_Validate(); + +} + + +typedef CLI_Input = ({ + + bool optHelp, + + bool optVersion, + + +}); + + +CLI_Input _resolve_CLI_Input(List restArgs) { + + bool var_optHelp = false; + + bool var_optVersion = false; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-version" || "-v": + if (!cut) { + lit = "true"; + + } + var_optVersion = _parseValue(var_optVersion.runtimeType, [lit]) as bool; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + optVersion: var_optVersion, + + + ); +} + + + + + + +class CLI_Generate { + Func? FUNC; + + final CLI_GenerateDart dart = CLI_GenerateDart(); + + final CLI_GenerateDocs docs = CLI_GenerateDocs(); + + final CLI_GenerateGolang golang = CLI_GenerateGolang(); + + final CLI_GeneratePython3 python3 = CLI_GeneratePython3(); + +} + + +typedef CLI_Generate_Input = ({ + + bool optHelp, + + +}); + + +CLI_Generate_Input _resolve_CLI_Generate_Input(List restArgs) { + + bool var_optHelp = false; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + + ); +} + + + +class CLI_GenerateDart { + Func? FUNC; + +} + + +typedef CLI_GenerateDart_Input = ({ + + bool optHelp, + + String optOutPath, + + String optSchemaPath, + + +}); + + +CLI_GenerateDart_Input _resolve_CLI_GenerateDart_Input(List restArgs) { + + bool var_optHelp = false; + + String var_optOutPath = ""; + + String var_optSchemaPath = ""; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-out-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optOutPath = _parseValue(var_optOutPath.runtimeType, [lit]) as String; + + case "-schema-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optSchemaPath = _parseValue(var_optSchemaPath.runtimeType, [lit]) as String; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + optOutPath: var_optOutPath, + + optSchemaPath: var_optSchemaPath, + + + ); +} + + + +class CLI_GenerateDocs { + Func? FUNC; + +} + + +typedef CLI_GenerateDocs_Input = ({ + + bool optAll, + + String optFormat, + + bool optHelp, + + String optOutPath, + + String optSchemaPath, + + + List argSubcommands, + +}); + + +CLI_GenerateDocs_Input _resolve_CLI_GenerateDocs_Input(List restArgs) { + + bool var_optAll = false; + + String var_optFormat = "text"; + + bool var_optHelp = false; + + String var_optOutPath = ""; + + String var_optSchemaPath = ""; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-all" || "-a": + if (!cut) { + lit = "true"; + + } + var_optAll = _parseValue(var_optAll.runtimeType, [lit]) as bool; + + case "-format" || "-f": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optFormat = _parseValue(var_optFormat.runtimeType, [lit]) as String; + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-out-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optOutPath = _parseValue(var_optOutPath.runtimeType, [lit]) as String; + + case "-schema-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optSchemaPath = _parseValue(var_optSchemaPath.runtimeType, [lit]) as String; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + + if (arguments.length <= 0 - 1) { + throw Exception("too few arguments"); + } + List var_argSubcommands = _parseValue(List, arguments.sublist(0)) as List; + + + + + return ( + + optAll: var_optAll, + + optFormat: var_optFormat, + + optHelp: var_optHelp, + + optOutPath: var_optOutPath, + + optSchemaPath: var_optSchemaPath, + + + argSubcommands: var_argSubcommands, + + ); +} + + + +class CLI_GenerateGolang { + Func? FUNC; + +} + + +typedef CLI_GenerateGolang_Input = ({ + + bool optHelp, + + String optOutPath, + + String optPackage, + + String optSchemaPath, + + +}); + + +CLI_GenerateGolang_Input _resolve_CLI_GenerateGolang_Input(List restArgs) { + + bool var_optHelp = false; + + String var_optOutPath = ""; + + String var_optPackage = "main"; + + String var_optSchemaPath = ""; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-out-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optOutPath = _parseValue(var_optOutPath.runtimeType, [lit]) as String; + + case "-package": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optPackage = _parseValue(var_optPackage.runtimeType, [lit]) as String; + + case "-schema-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optSchemaPath = _parseValue(var_optSchemaPath.runtimeType, [lit]) as String; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + optOutPath: var_optOutPath, + + optPackage: var_optPackage, + + optSchemaPath: var_optSchemaPath, + + + ); +} + + + +class CLI_GeneratePython3 { + Func? FUNC; + +} + + +typedef CLI_GeneratePython3_Input = ({ + + bool optHelp, + + String optOutPath, + + String optSchemaPath, + + +}); + + +CLI_GeneratePython3_Input _resolve_CLI_GeneratePython3_Input(List restArgs) { + + bool var_optHelp = false; + + String var_optOutPath = ""; + + String var_optSchemaPath = ""; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-out-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optOutPath = _parseValue(var_optOutPath.runtimeType, [lit]) as String; + + case "-schema-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optSchemaPath = _parseValue(var_optSchemaPath.runtimeType, [lit]) as String; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + optOutPath: var_optOutPath, + + optSchemaPath: var_optSchemaPath, + + + ); +} + + + +class CLI_List { + Func? FUNC; + +} + + +typedef CLI_List_Input = ({ + + bool optHelp, + + String optSchemaPath, + + +}); + + +CLI_List_Input _resolve_CLI_List_Input(List restArgs) { + + bool var_optHelp = false; + + String var_optSchemaPath = ""; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-schema-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optSchemaPath = _parseValue(var_optSchemaPath.runtimeType, [lit]) as String; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + optSchemaPath: var_optSchemaPath, + + + ); +} + + + +class CLI_Validate { + Func? FUNC; + +} + + +typedef CLI_Validate_Input = ({ + + bool optHelp, + + String optSchemaPath, + + +}); + + +CLI_Validate_Input _resolve_CLI_Validate_Input(List restArgs) { + + bool var_optHelp = false; + + String var_optSchemaPath = ""; + + List arguments = []; + for (int idx = 0; idx < restArgs.length; ++idx) { + final arg = restArgs[idx]; + if (arg == "--") { + arguments.addAll(restArgs.sublist(idx + 1)); + break; + } + if (!arg.startsWith("-")) { + arguments.add(arg); + continue; + } + final eqPos = arg.indexOf("="); + final cut = eqPos >= 0; + final optName = cut?arg.substring(0,eqPos) : arg; + var lit = cut?arg.substring(eqPos+1) : ""; + + switch (optName) { + + case "-help" || "-h": + if (!cut) { + lit = "true"; + + } + var_optHelp = _parseValue(var_optHelp.runtimeType, [lit]) as bool; + + case "-schema-path": + if (!cut) { + throw Exception("value is not specified to option ${optName}"); + + } + var_optSchemaPath = _parseValue(var_optSchemaPath.runtimeType, [lit]) as String; + + default: + throw Exception("unknown option ${optName}"); + } + } + + + + return ( + + optHelp: var_optHelp, + + optSchemaPath: var_optSchemaPath, + + + ); +} + + + + +void run(CLI cli, List args) { + var (subcommandPath: subcommandPath, restArgs: restArgs) = _resolveSubcommand(args); + switch (subcommandPath.join(" ")) { + + case "": + final funcMethod = cli.FUNC; + if (funcMethod == null) { + throw Exception("'${ "" }' is unsupported: cli.FUNC not assigned"); + } + + CLI_Input? input; + Exception? err; + try { + input = _resolve_CLI_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + + case "generate": + final funcMethod = cli.generate.FUNC; + if (funcMethod == null) { + throw Exception("'${ "generate" }' is unsupported: cli.generate.FUNC not assigned"); + } + + CLI_Generate_Input? input; + Exception? err; + try { + input = _resolve_CLI_Generate_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + case "generate dart": + final funcMethod = cli.generate.dart.FUNC; + if (funcMethod == null) { + throw Exception("'${ "generate dart" }' is unsupported: cli.generate.dart.FUNC not assigned"); + } + + CLI_GenerateDart_Input? input; + Exception? err; + try { + input = _resolve_CLI_GenerateDart_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + case "generate docs": + final funcMethod = cli.generate.docs.FUNC; + if (funcMethod == null) { + throw Exception("'${ "generate docs" }' is unsupported: cli.generate.docs.FUNC not assigned"); + } + + CLI_GenerateDocs_Input? input; + Exception? err; + try { + input = _resolve_CLI_GenerateDocs_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + case "generate golang": + final funcMethod = cli.generate.golang.FUNC; + if (funcMethod == null) { + throw Exception("'${ "generate golang" }' is unsupported: cli.generate.golang.FUNC not assigned"); + } + + CLI_GenerateGolang_Input? input; + Exception? err; + try { + input = _resolve_CLI_GenerateGolang_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + case "generate python3": + final funcMethod = cli.generate.python3.FUNC; + if (funcMethod == null) { + throw Exception("'${ "generate python3" }' is unsupported: cli.generate.python3.FUNC not assigned"); + } + + CLI_GeneratePython3_Input? input; + Exception? err; + try { + input = _resolve_CLI_GeneratePython3_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + case "list": + final funcMethod = cli.list.FUNC; + if (funcMethod == null) { + throw Exception("'${ "list" }' is unsupported: cli.list.FUNC not assigned"); + } + + CLI_List_Input? input; + Exception? err; + try { + input = _resolve_CLI_List_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + case "validate": + final funcMethod = cli.validate.FUNC; + if (funcMethod == null) { + throw Exception("'${ "validate" }' is unsupported: cli.validate.FUNC not assigned"); + } + + CLI_Validate_Input? input; + Exception? err; + try { + input = _resolve_CLI_Validate_Input(restArgs); + } on Exception catch (e) { + err = e; + } + funcMethod(subcommandPath, input, err); + + } +} + + +({List subcommandPath, List restArgs}) _resolveSubcommand(List args) { + final subcommandSet = { + "": true, + "generate": true, + "generate dart": true, + "generate docs": true, + "generate golang": true, + "generate python3": true, + "list": true, + "validate": true, + + }; + + List subcommandPath = []; + for (var arg in args) { + if (arg == "--") { + break; + } + final pathLiteral = ([]..addAll(subcommandPath)..add(arg)).join(" "); + if (!subcommandSet.containsKey(pathLiteral)) { + break; + } + + subcommandPath.add(arg); + } + + return (subcommandPath: subcommandPath, restArgs: args.sublist(subcommandPath.length)); +} + +dynamic _parseValue(Type t, List strValue) { + switch (t) { + case const (List): + return strValue.map((s)=>_parseValue(bool, [s]) as bool).toList(); + case const (List): + return strValue.map((s)=>_parseValue(int, [s]) as int).toList(); + case const (List): + return strValue.map((s)=>_parseValue(double, [s]) as double).toList(); + case const (List): + return strValue.map((s)=>_parseValue(String, [s]) as String).toList(); + case bool when strValue.length == 1: + return switch(strValue[0]) { + "1" || "t" || "T" || "true" || "TRUE" || "True" => true, + "0" || "f" || "F" || "false" || "FALSE" || "False" => false, + _ => throw Exception("invalid boolean value: ${strValue[0]}"), + }; + case int when strValue.length == 1: + return int.parse(strValue[0]); + case double when strValue.length == 1: + return double.parse(strValue[0]); + case String when strValue.length == 1: + return strValue[0]; + } + + throw Exception("invalid type: ${t}"); +} + + +String getDoc(List subcommands) { + switch (subcommands.join(" ")) { + + case "": + return "cyamli (v1.1.5)\n\ncyamli\n\n Description:\n A command line tool to generate CLI for your app from YAML-based schema.\n\n Syntax:\n \$ cyamli [