This commit is contained in:
PiotrP 2016-12-15 10:27:30 -08:00 committed by Livar Cunha
parent a1afcf6e12
commit 1b8daa6ba3
2 changed files with 84 additions and 27 deletions

View file

@ -112,7 +112,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
var lastArg = Arguments.LastOrDefault(); var lastArg = Arguments.LastOrDefault();
if (lastArg != null && lastArg.MultipleValues) if (lastArg != null && lastArg.MultipleValues)
{ {
var message = string.Format("The last argument '{0}' accepts multiple values. No more argument can be added.", var message = string.Format(LocalizableStrings.LastArgumentMultiValueError,
lastArg.Name); lastArg.Name);
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
@ -266,7 +266,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
command.ShowHint(); command.ShowHint();
throw new CommandParsingException(command, throw new CommandParsingException(command,
$"Unexpected value '{optionComponents[1]}' for option '{optionName}'"); String.Format(LocalizableStrings.UnexpectedValueForOptionError, optionComponents[1], optionName));
} }
} }
else else
@ -284,7 +284,9 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (!option.TryParse(arg)) if (!option.TryParse(arg))
{ {
command.ShowHint(); command.ShowHint();
throw new CommandParsingException(command, $"Unexpected value '{arg}' for option '{optionName}'"); throw new CommandParsingException(
command,
String.Format(LocalizableStrings.UnexpectedValueForOptionError, arg, optionName));
} }
} }
@ -312,7 +314,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
// Help option is special because we stop parsing once we see it // Help option is special because we stop parsing once we see it
// So we store it separately for further use // So we store it separately for further use
OptionHelp = Option(template, "Show help information", CommandOptionType.NoValue); OptionHelp = Option(template, LocalizableStrings.ShowHelpInfo, CommandOptionType.NoValue);
return OptionHelp; return OptionHelp;
} }
@ -338,7 +340,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
// Version option is special because we stop parsing once we see it // Version option is special because we stop parsing once we see it
// So we store it separately for further use // So we store it separately for further use
OptionVersion = Option(template, "Show version information", CommandOptionType.NoValue); OptionVersion = Option(template, LocalizableStrings.ShowVersionInfo, CommandOptionType.NoValue);
ShortVersionGetter = shortFormVersionGetter; ShortVersionGetter = shortFormVersionGetter;
LongVersionGetter = longFormVersionGetter ?? shortFormVersionGetter; LongVersionGetter = longFormVersionGetter ?? shortFormVersionGetter;
@ -350,14 +352,14 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
if (OptionHelp != null) if (OptionHelp != null)
{ {
Console.WriteLine(string.Format("Specify --{0} for a list of available options and commands.", OptionHelp.LongName)); Console.WriteLine(string.Format(LocalizableStrings.ShowHintInfo, OptionHelp.LongName));
} }
} }
// Show full help // Show full help
public void ShowHelp(string commandName = null) public void ShowHelp(string commandName = null)
{ {
var headerBuilder = new StringBuilder("Usage:"); var headerBuilder = new StringBuilder(LocalizableStrings.UsageHeader);
var usagePrefixLength = headerBuilder.Length; var usagePrefixLength = headerBuilder.Length;
for (var cmd = this; cmd != null; cmd = cmd.Parent) for (var cmd = this; cmd != null; cmd = cmd.Parent)
{ {
@ -365,11 +367,11 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (cmd != this && cmd.Arguments.Any()) if (cmd != this && cmd.Arguments.Any())
{ {
var args = string.Join(" ", cmd.Arguments.Select(arg => arg.Name)); var args = string.Join(" ", cmd.Arguments.Select(arg => arg.Name));
headerBuilder.Insert(usagePrefixLength, string.Format(" {0} {1}", cmd.Name, args)); headerBuilder.Insert(usagePrefixLength, string.Format(LocalizableStrings.UsageItemWithArgs, cmd.Name, args));
} }
else else
{ {
headerBuilder.Insert(usagePrefixLength, string.Format(" {0}", cmd.Name)); headerBuilder.Insert(usagePrefixLength, string.Format(LocalizableStrings.UsageItemWithoutArgs, cmd.Name));
} }
} }
@ -385,7 +387,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (target != null) if (target != null)
{ {
headerBuilder.AppendFormat(" {0}", commandName); headerBuilder.AppendFormat(LocalizableStrings.CommandItem, commandName);
} }
else else
{ {
@ -407,13 +409,13 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
if (cmd == target) if (cmd == target)
{ {
headerBuilder.Append(" [arguments]"); headerBuilder.Append(LocalizableStrings.UsageArgumentsToken);
} }
if (argumentsBuilder.Length == 0) if (argumentsBuilder.Length == 0)
{ {
argumentsBuilder.AppendLine(); argumentsBuilder.AppendLine();
argumentsBuilder.AppendLine("Arguments:"); argumentsBuilder.AppendLine(LocalizableStrings.UsageArgumentsHeader);
} }
maxArgLen = Math.Max(maxArgLen, MaxArgumentLength(cmd.Arguments)); maxArgLen = Math.Max(maxArgLen, MaxArgumentLength(cmd.Arguments));
@ -424,7 +426,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
if (cmd.Arguments.Any()) if (cmd.Arguments.Any())
{ {
var outputFormat = " {0}{1}"; var outputFormat = LocalizableStrings.UsageArgumentItem;
foreach (var arg in cmd.Arguments) foreach (var arg in cmd.Arguments)
{ {
argumentsBuilder.AppendFormat( argumentsBuilder.AppendFormat(
@ -438,12 +440,12 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (target.Options.Any()) if (target.Options.Any())
{ {
headerBuilder.Append(" [options]"); headerBuilder.Append(LocalizableStrings.UsageOptionsToken);
optionsBuilder.AppendLine(); optionsBuilder.AppendLine();
optionsBuilder.AppendLine("Options:"); optionsBuilder.AppendLine(LocalizableStrings.UsageOptionsHeader);
var maxOptLen = MaxOptionTemplateLength(target.Options); var maxOptLen = MaxOptionTemplateLength(target.Options);
var outputFormat = string.Format(" {{0, -{0}}}{{1}}", maxOptLen + 2); var outputFormat = string.Format(LocalizableStrings.UsageOptionsItem, maxOptLen + 2);
foreach (var opt in target.Options) foreach (var opt in target.Options)
{ {
optionsBuilder.AppendFormat(outputFormat, opt.Template, opt.Description); optionsBuilder.AppendFormat(outputFormat, opt.Template, opt.Description);
@ -453,12 +455,12 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (target.Commands.Any()) if (target.Commands.Any())
{ {
headerBuilder.Append(" [command]"); headerBuilder.Append(LocalizableStrings.UsageCommandToken);
commandsBuilder.AppendLine(); commandsBuilder.AppendLine();
commandsBuilder.AppendLine("Commands:"); commandsBuilder.AppendLine(LocalizableStrings.UsageCommandsHeader);
var maxCmdLen = MaxCommandLength(target.Commands); var maxCmdLen = MaxCommandLength(target.Commands);
var outputFormat = string.Format(" {{0, -{0}}}{{1}}", maxCmdLen + 2); var outputFormat = string.Format(LocalizableStrings.UsageCommandsItem, maxCmdLen + 2);
foreach (var cmd in target.Commands.OrderBy(c => c.Name)) foreach (var cmd in target.Commands.OrderBy(c => c.Name))
{ {
commandsBuilder.AppendFormat(outputFormat, cmd.Name, cmd.Description); commandsBuilder.AppendFormat(outputFormat, cmd.Name, cmd.Description);
@ -468,7 +470,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (OptionHelp != null) if (OptionHelp != null)
{ {
commandsBuilder.AppendLine(); commandsBuilder.AppendLine();
commandsBuilder.AppendFormat("Use \"{0} [command] --help\" for more information about a command.", Name); commandsBuilder.AppendFormat(LocalizableStrings.UsageCommandsDetailHelp, Name);
commandsBuilder.AppendLine(); commandsBuilder.AppendLine();
} }
} }
@ -477,18 +479,18 @@ namespace Microsoft.DotNet.Cli.CommandLine
{ {
if (target.AllowArgumentSeparator) if (target.AllowArgumentSeparator)
{ {
headerBuilder.Append(" [[--] <arg>...]]"); headerBuilder.Append(LocalizableStrings.UsageCommandAdditionalArgs);
} }
else else
{ {
headerBuilder.Append(" [args]"); headerBuilder.Append(LocalizableStrings.UsageCommandArgs);
} }
if (!string.IsNullOrEmpty(target.ArgumentSeparatorHelpText)) if (!string.IsNullOrEmpty(target.ArgumentSeparatorHelpText))
{ {
argumentSeparatorBuilder.AppendLine(); argumentSeparatorBuilder.AppendLine();
argumentSeparatorBuilder.AppendLine("Args:"); argumentSeparatorBuilder.AppendLine(LocalizableStrings.UsageCommandsAdditionalArgsHeader);
argumentSeparatorBuilder.AppendLine($" {target.ArgumentSeparatorHelpText}"); argumentSeparatorBuilder.AppendLine(String.Format(LocalizableStrings.UsageCommandsAdditionalArgsItem, target.ArgumentSeparatorHelpText));
argumentSeparatorBuilder.AppendLine(); argumentSeparatorBuilder.AppendLine();
} }
} }
@ -515,7 +517,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
public string GetFullNameAndVersion() public string GetFullNameAndVersion()
{ {
return ShortVersionGetter == null ? FullName : string.Format("{0} {1}", FullName, ShortVersionGetter()); return ShortVersionGetter == null ? FullName : string.Format(LocalizableStrings.ShortVersionTemplate, FullName, ShortVersionGetter());
} }
public void ShowRootCommandFullNameAndVersion() public void ShowRootCommandFullNameAndVersion()
@ -565,7 +567,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (command._throwOnUnexpectedArg) if (command._throwOnUnexpectedArg)
{ {
command.ShowHint(); command.ShowHint();
throw new CommandParsingException(command, $"Unrecognized {argTypeName} '{args[index]}'"); throw new CommandParsingException(command, String.Format(LocalizableStrings.UnexpectedArgumentError, argTypeName, args[index]));
} }
else else
{ {
@ -612,7 +614,7 @@ namespace Microsoft.DotNet.Cli.CommandLine
if (!File.Exists(fileName)) if (!File.Exists(fileName))
{ {
throw new InvalidOperationException($"Response file '{fileName}' doesn't exist."); throw new InvalidOperationException(String.Format(LocalizableStrings.ResponseFileNotFoundError, fileName));
} }
return File.ReadLines(fileName); return File.ReadLines(fileName);

View file

@ -0,0 +1,55 @@
namespace Microsoft.DotNet.Cli.CommandLine
{
internal class LocalizableStrings
{
public const string LastArgumentMultiValueError = "The last argument '{0}' accepts multiple values. No more argument can be added.";
public const string UnexpectedValueForOptionError = "Unexpected value '{0}' for option '{1}'";
public const string UnexpectedArgumentError = "Unrecognized {0} '{1}'";
public const string ResponseFileNotFoundError = "Response file '{0}' doesn't exist.";
public const string ShowHelpInfo = "Show help information";
public const string ShowVersionInfo = "Show version information";
public const string ShowHintInfo = "Specify --{0} for a list of available options and commands.";
public const string UsageHeader = "Usage:";
public const string UsageItemWithArgs = " {0} {1}";
public const string UsageArgumentsToken = " [arguments]";
public const string UsageArgumentsHeader = "Arguments:";
public const string UsageArgumentItem = " {0}{1}";
public const string UsageOptionsToken = " [options]";
public const string UsageOptionsHeader = "Options:";
public const string UsageOptionsItem = " {{0, -{0}}}{{1}}";
public const string UsageCommandToken = " [command]";
public const string UsageCommandsHeader = "Commands:";
public const string UsageCommandsItem = " {{0, -{0}}}{{1}}";
public const string UsageCommandsDetailHelp = "Use \"{0} [command] --help\" for more information about a command.";
public const string UsageCommandArgs = " [args]";
public const string UsageCommandAdditionalArgs = " [[--] <arg>...]]";
public const string UsageCommandsAdditionalArgsHeader = "Args:";
public const string UsageCommandsAdditionalArgsItem = " {0}";
public const string CommandItem = " {0}";
public const string ShortVersionTemplate = "{0} {1}";
}
}