|
1 | 1 | // Copyright (c) Pomelo Foundation. All rights reserved. |
2 | 2 | // Licensed under the MIT License |
3 | 3 |
|
4 | | -using Microsoft.Extensions.CommandLineUtils; |
5 | 4 | using MySqlConnector; |
6 | 5 | using System; |
| 6 | +using System.CommandLine; |
7 | 7 | using System.Data; |
8 | 8 | using System.IO; |
9 | 9 | using System.Reflection; |
@@ -42,92 +42,104 @@ public int Run(string[] args) |
42 | 42 | var description = "Creates table and indexes in MySQL Server database " + |
43 | 43 | "to be used for distributed caching"; |
44 | 44 |
|
45 | | - var cliApp = new CommandLineApplication(); |
46 | | - cliApp.Error = Error; |
47 | | - cliApp.Out = Out; |
| 45 | + var rootCommand = new RootCommand(description); |
48 | 46 |
|
49 | | - cliApp.FullName = "MySQL Server Cache Command Line Tool"; |
50 | | - cliApp.Name = "dotnet-mysql-cache"; |
51 | | - cliApp.Description = description; |
52 | | - cliApp.ShortVersionGetter = () => |
| 47 | + // Create command |
| 48 | + var createCommand = new Command("create", description); |
| 49 | + |
| 50 | + var createConnectionStringArg = new Argument<string>("connectionString") |
53 | 51 | { |
54 | | - var assembly = typeof(Program).GetTypeInfo().Assembly; |
55 | | - var infoVersion = assembly |
56 | | - ?.GetCustomAttribute<AssemblyInformationalVersionAttribute>() |
57 | | - ?.InformationalVersion; |
58 | | - return string.IsNullOrWhiteSpace(infoVersion) |
59 | | - ? assembly?.GetName().Version.ToString() |
60 | | - : infoVersion; |
| 52 | + Description = "The connection string to connect to the database." |
61 | 53 | }; |
62 | | - cliApp.HelpOption("-?|-h|--help"); |
63 | | - |
64 | | - cliApp.Command("create", command => |
| 54 | + |
| 55 | + var createDatabaseNameOpt = new Option<string>("--databaseName") |
| 56 | + { |
| 57 | + Description = "Name of the database. If not existing or set in connection string." |
| 58 | + }; |
| 59 | + createDatabaseNameOpt.Aliases.Add("-d"); |
| 60 | + |
| 61 | + var createTableNameArg = new Argument<string>("tableName") |
65 | 62 | { |
66 | | - command.Error = Error;//internal command Out/Error are not yet changed, possible shortcoming |
67 | | - command.Out = Out; |
68 | | - |
69 | | - command.Description = description; |
70 | | - var connectionStringArg = command.Argument( |
71 | | - "[connectionString]", |
72 | | - "The connection string to connect to the database."); |
73 | | - //var databaseNameArg = command.Argument("[databaseName]", "Name of the database."); |
74 | | - var databaseNameOpt = command.Option("-d|--databaseName", "Name of the database. If not existing or set in connection string.", CommandOptionType.SingleValue); |
75 | | - var tableNameArg = command.Argument("[tableName]", "Name of the table to be created."); |
76 | | - command.HelpOption("-?|-h|--help"); |
77 | | - |
78 | | - command.OnExecute(async () => |
| 63 | + Description = "Name of the table to be created." |
| 64 | + }; |
| 65 | + |
| 66 | + createCommand.Arguments.Add(createConnectionStringArg); |
| 67 | + createCommand.Options.Add(createDatabaseNameOpt); |
| 68 | + createCommand.Arguments.Add(createTableNameArg); |
| 69 | + |
| 70 | + createCommand.SetAction(parseResult => |
| 71 | + { |
| 72 | + var connectionString = parseResult.GetValue(createConnectionStringArg); |
| 73 | + var databaseName = parseResult.GetValue(createDatabaseNameOpt); |
| 74 | + var tableName = parseResult.GetValue(createTableNameArg); |
| 75 | + |
| 76 | + if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(tableName)) |
79 | 77 | { |
80 | | - if (string.IsNullOrEmpty(connectionStringArg.Value) |
81 | | - || string.IsNullOrEmpty(tableNameArg.Value)) |
82 | | - { |
83 | | - await Error.WriteLineAsync("Invalid input"); |
84 | | - cliApp.ShowHelp(command.Name); |
85 | | - return 2; |
86 | | - } |
| 78 | + Error.WriteLine("Invalid input"); |
| 79 | + Error.WriteLine("Usage: create <connectionString> <tableName> [--databaseName <name>]"); |
| 80 | + return 2; |
| 81 | + } |
87 | 82 |
|
88 | | - _connectionString = connectionStringArg.Value; |
89 | | - _databaseName = databaseNameOpt.Value(); |
90 | | - _tableName = tableNameArg.Value; |
| 83 | + _connectionString = connectionString; |
| 84 | + _databaseName = databaseName; |
| 85 | + _tableName = tableName; |
91 | 86 |
|
92 | | - return await CreateTableAndIndexes(); |
93 | | - }); |
| 87 | + return CreateTableAndIndexes().GetAwaiter().GetResult(); |
94 | 88 | }); |
95 | 89 |
|
96 | | - cliApp.Command("script", command => |
| 90 | + // Script command |
| 91 | + var scriptCommand = new Command("script", "Generate creation script"); |
| 92 | + |
| 93 | + var scriptDatabaseNameOpt = new Option<string>("--databaseName") |
97 | 94 | { |
98 | | - command.Error = Error;//internal command Out/Error are not yet changed, possible shortcoming |
99 | | - command.Out = Out; |
100 | | - |
101 | | - command.Description = "Generate creation script"; |
102 | | - //var databaseNameArg = command.Argument("[databaseName]", "Name of the database."); |
103 | | - var databaseNameOpt = command.Option("-d|--databaseName", "Name of the database. If not existing or set in connection string.", CommandOptionType.SingleValue); |
104 | | - var tableNameArg = command.Argument("[tableName]", "Name of the table to be created."); |
105 | | - command.HelpOption("-?|-h|--help"); |
| 95 | + Description = "Name of the database. If not existing or set in connection string." |
| 96 | + }; |
| 97 | + scriptDatabaseNameOpt.Aliases.Add("-d"); |
| 98 | + |
| 99 | + var scriptTableNameArg = new Argument<string>("tableName") |
| 100 | + { |
| 101 | + Description = "Name of the table to be created." |
| 102 | + }; |
| 103 | + |
| 104 | + scriptCommand.Options.Add(scriptDatabaseNameOpt); |
| 105 | + scriptCommand.Arguments.Add(scriptTableNameArg); |
| 106 | + |
| 107 | + scriptCommand.SetAction(parseResult => |
| 108 | + { |
| 109 | + var databaseName = parseResult.GetValue(scriptDatabaseNameOpt); |
| 110 | + var tableName = parseResult.GetValue(scriptTableNameArg); |
106 | 111 |
|
107 | | - command.OnExecute(async () => |
| 112 | + if (string.IsNullOrEmpty(tableName)) |
108 | 113 | { |
109 | | - if (string.IsNullOrEmpty(tableNameArg.Value)) |
110 | | - { |
111 | | - await Error.WriteLineAsync("Invalid input"); |
112 | | - cliApp.ShowHelp(command.Name); |
113 | | - return 2; |
114 | | - } |
| 114 | + Error.WriteLine("Invalid input"); |
| 115 | + Error.WriteLine("Usage: script <tableName> [--databaseName <name>]"); |
| 116 | + return 2; |
| 117 | + } |
115 | 118 |
|
116 | | - _databaseName = databaseNameOpt.Value(); |
117 | | - _tableName = tableNameArg.Value; |
| 119 | + _databaseName = databaseName; |
| 120 | + _tableName = tableName; |
118 | 121 |
|
119 | | - return await GenerateScript(); |
120 | | - }); |
| 122 | + return GenerateScript().GetAwaiter().GetResult(); |
121 | 123 | }); |
122 | 124 |
|
123 | | - // Show help information if no subcommand/option was specified. |
124 | | - cliApp.OnExecute(() => |
125 | | - { |
126 | | - cliApp.ShowHelp(); |
127 | | - return 2; |
128 | | - }); |
| 125 | + rootCommand.Subcommands.Add(createCommand); |
| 126 | + rootCommand.Subcommands.Add(scriptCommand); |
129 | 127 |
|
130 | | - return cliApp.Execute(args); |
| 128 | + // Temporarily redirect Console.Out and Console.Error to custom streams |
| 129 | + // so System.CommandLine can write to them |
| 130 | + var originalOut = Console.Out; |
| 131 | + var originalError = Console.Error; |
| 132 | + try |
| 133 | + { |
| 134 | + Console.SetOut(Out); |
| 135 | + Console.SetError(Error); |
| 136 | + return rootCommand.Parse(args).Invoke(); |
| 137 | + } |
| 138 | + finally |
| 139 | + { |
| 140 | + Console.SetOut(originalOut); |
| 141 | + Console.SetError(originalError); |
| 142 | + } |
131 | 143 | } |
132 | 144 | catch (Exception ex) |
133 | 145 | { |
|
0 commit comments