diff --git a/src/CodeFormatter/CommandLineOptions.cs b/src/CodeFormatter/CommandLineOptions.cs new file mode 100644 index 00000000..9d0b0a98 --- /dev/null +++ b/src/CodeFormatter/CommandLineOptions.cs @@ -0,0 +1,62 @@ +using System.Collections.Immutable; + +namespace CodeFormatter +{ + public sealed class CommandLineOptions + { + public static readonly CommandLineOptions ListRules = new CommandLineOptions( + Operation.ListRules, + ImmutableArray.Empty, + ImmutableArray.Empty, + ImmutableDictionary.Empty, + ImmutableArray.Empty, + ImmutableArray.Empty, + null, + allowTables: false, + verbose: false); + + public static readonly CommandLineOptions ShowHelp = new CommandLineOptions( + Operation.ShowHelp, + ImmutableArray.Empty, + ImmutableArray.Empty, + ImmutableDictionary.Empty, + ImmutableArray.Empty, + ImmutableArray.Empty, + null, + allowTables: false, + verbose: false); + + + public readonly Operation Operation; + public readonly ImmutableArray PreprocessorConfigurations; + public readonly ImmutableArray CopyrightHeader; + public readonly ImmutableDictionary RuleMap; + public readonly ImmutableArray FormatTargets; + public readonly ImmutableArray FileNames; + public readonly string Language; + public readonly bool AllowTables; + public readonly bool Verbose; + + public CommandLineOptions( + Operation operation, + ImmutableArray preprocessorConfigurations, + ImmutableArray copyrightHeader, + ImmutableDictionary ruleMap, + ImmutableArray formatTargets, + ImmutableArray fileNames, + string language, + bool allowTables, + bool verbose) + { + Operation = operation; + PreprocessorConfigurations = preprocessorConfigurations; + CopyrightHeader = copyrightHeader; + RuleMap = ruleMap; + FileNames = fileNames; + FormatTargets = formatTargets; + Language = language; + AllowTables = allowTables; + Verbose = verbose; + } + } +} diff --git a/src/CodeFormatter/CommandLineParseResult.cs b/src/CodeFormatter/CommandLineParseResult.cs new file mode 100644 index 00000000..23cf7022 --- /dev/null +++ b/src/CodeFormatter/CommandLineParseResult.cs @@ -0,0 +1,54 @@ +using System.Diagnostics; + +namespace CodeFormatter +{ + public sealed class CommandLineParseResult + { + private readonly CommandLineOptions _options; + private readonly string _error; + + public bool IsSuccess + { + get { return _options != null; } + } + + public bool IsError + { + get { return !IsSuccess; } + } + + public CommandLineOptions Options + { + get + { + Debug.Assert(IsSuccess); + return _options; + } + } + + public string Error + { + get + { + Debug.Assert(IsError); + return _error; + } + } + + private CommandLineParseResult(CommandLineOptions options = null, string error = null) + { + _options = options; + _error = error; + } + + public static CommandLineParseResult CreateSuccess(CommandLineOptions options) + { + return new CommandLineParseResult(options: options); + } + + public static CommandLineParseResult CreateError(string error) + { + return new CommandLineParseResult(error: error); + } + } +} diff --git a/src/CodeFormatter/CommandLineParser.cs b/src/CodeFormatter/CommandLineParser.cs index 72aaa123..87d9789d 100644 --- a/src/CodeFormatter/CommandLineParser.cs +++ b/src/CodeFormatter/CommandLineParser.cs @@ -3,129 +3,10 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Diagnostics; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace CodeFormatter { - public enum Operation - { - Format, - ListRules, - ShowHelp - } - - public sealed class CommandLineOptions - { - public static readonly CommandLineOptions ListRules = new CommandLineOptions( - Operation.ListRules, - ImmutableArray.Empty, - ImmutableArray.Empty, - ImmutableDictionary.Empty, - ImmutableArray.Empty, - ImmutableArray.Empty, - null, - allowTables: false, - verbose: false); - - public static readonly CommandLineOptions ShowHelp = new CommandLineOptions( - Operation.ShowHelp, - ImmutableArray.Empty, - ImmutableArray.Empty, - ImmutableDictionary.Empty, - ImmutableArray.Empty, - ImmutableArray.Empty, - null, - allowTables: false, - verbose: false); - - - public readonly Operation Operation; - public readonly ImmutableArray PreprocessorConfigurations; - public readonly ImmutableArray CopyrightHeader; - public readonly ImmutableDictionary RuleMap; - public readonly ImmutableArray FormatTargets; - public readonly ImmutableArray FileNames; - public readonly string Language; - public readonly bool AllowTables; - public readonly bool Verbose; - - public CommandLineOptions( - Operation operation, - ImmutableArray preprocessorConfigurations, - ImmutableArray copyrightHeader, - ImmutableDictionary ruleMap, - ImmutableArray formatTargets, - ImmutableArray fileNames, - string language, - bool allowTables, - bool verbose) - { - Operation = operation; - PreprocessorConfigurations = preprocessorConfigurations; - CopyrightHeader = copyrightHeader; - RuleMap = ruleMap; - FileNames = fileNames; - FormatTargets = formatTargets; - Language = language; - AllowTables = allowTables; - Verbose = verbose; - } - } - - public sealed class CommandLineParseResult - { - private readonly CommandLineOptions _options; - private readonly string _error; - - public bool IsSuccess - { - get { return _options != null; } - } - - public bool IsError - { - get { return !IsSuccess; } - } - - public CommandLineOptions Options - { - get - { - Debug.Assert(IsSuccess); - return _options; - } - } - - public string Error - { - get - { - Debug.Assert(IsError); - return _error; - } - } - - private CommandLineParseResult(CommandLineOptions options = null, string error = null) - { - _options = options; - _error = error; - } - - public static CommandLineParseResult CreateSuccess(CommandLineOptions options) - { - return new CommandLineParseResult(options: options); - } - - public static CommandLineParseResult CreateError(string error) - { - return new CommandLineParseResult(error: error); - } - } - public static class CommandLineParser { private const string FileSwitch = "/file:"; @@ -184,9 +65,8 @@ public static CommandLineParseResult Parse(string[] args) var allowTables = false; var verbose = false; - for (int i = 0; i < args.Length; i++) + foreach (var arg in args) { - string arg = args[i]; if (arg.StartsWith(ConfigSwitch, comparison)) { var all = arg.Substring(ConfigSwitch.Length); @@ -209,10 +89,7 @@ public static CommandLineParseResult Parse(string[] args) } catch (Exception ex) { - string error = string.Format("Could not read {0}{1}{2}", - fileName, - Environment.NewLine, - ex.Message); + string error = $"Could not read {fileName}{Environment.NewLine}{ex.Message}"; return CommandLineParseResult.CreateError(error); } } @@ -257,7 +134,8 @@ public static CommandLineParseResult Parse(string[] args) { return CommandLineParseResult.CreateSuccess(CommandLineOptions.ListRules); } - else if (comparer.Equals(arg, "/?") || comparer.Equals(arg, "/help")) + else if (comparer.Equals(arg, "/?") || comparer.Equals(arg, "-?") || comparer.Equals(arg, "/help") || + comparer.Equals(arg, "-help") || comparer.Equals(arg, "--help")) { return CommandLineParseResult.CreateSuccess(CommandLineOptions.ShowHelp); } diff --git a/src/CodeFormatter/Operation.cs b/src/CodeFormatter/Operation.cs new file mode 100644 index 00000000..7cb7773b --- /dev/null +++ b/src/CodeFormatter/Operation.cs @@ -0,0 +1,9 @@ +namespace CodeFormatter +{ + public enum Operation + { + Format = 0, + ListRules = 1, + ShowHelp = 2 + } +}