public static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler); Printer.PrinterStream printerStream = new Printer.PrinterStream(); Parser parser = new Parser(new Action<ParserSettings>( (ParserSettings p) => { p.CaseSensitive = false; p.IgnoreUnknownArguments = false; p.HelpWriter = printerStream; p.MutuallyExclusive = true; })); Options = new LogrOptions(); if (!parser.ParseArguments(args, Options)) { printerStream.Flush(); Printer.RestoreDefaults(); Environment.Exit(CommandLine.Parser.DefaultExitCodeFail); } Log log = new Log(Options.Repo, Options.LogFile, (Options.Limit > 0) ? Options.Limit : (int?)null); log.Update(); log.Serialize(); }
private static void PrintAllOptions(string[] args, Parser parser, Printer.PrinterStream printerStream) { // We didn't hit a verb, print the base options and then all the plugin ones printerStream.WriteLine((new Options()).GetUsage()); printerStream.Flush(); foreach (var pluginOptionsAndAssemblies in PluginOptionsAndAssemblies) { System.Reflection.MethodInfo helpOption = GetHelpMethod(pluginOptionsAndAssemblies.Item1); if (helpOption != null) { printerStream.WriteLine(); printerStream.WriteLine(helpOption.Invoke(pluginOptionsAndAssemblies.Item1, new object[0])); } else { Printer.PrintMessage("Warning - plugin #b#{0}## does not have built-in help.", pluginOptionsAndAssemblies.Item2.GetName().Name); } } }
static void Main(string[] args) { Versionr.Utilities.Misc.StartTimer(); try { string workingDirectoryPath = Environment.CurrentDirectory; var printerStream = new Printer.PrinterStream(); var nullstream = new System.IO.MemoryStream(); VersionOptions initalOpts = new VersionOptions(); CommandLine.Parser silentparser = new CommandLine.Parser(new Action <ParserSettings>( (ParserSettings p) => { p.CaseSensitive = true; p.IgnoreUnknownArguments = false; p.HelpWriter = new System.IO.StreamWriter(nullstream); p.MutuallyExclusive = true; })); CommandLine.Parser parser = new CommandLine.Parser(new Action <ParserSettings>( (ParserSettings p) => { p.CaseSensitive = true; p.IgnoreUnknownArguments = false; p.HelpWriter = printerStream; p.MutuallyExclusive = true; })); if (args.Length >= 1 && args[0] == "--version" && parser.ParseArguments(args, initalOpts) && initalOpts.Version) { Printer.WriteLineMessage("#b#Versionr## v{0} #q#{1}{2}", System.Reflection.Assembly.GetCallingAssembly().GetName().Version, Utilities.MultiArchPInvoke.IsX64 ? "x64" : "x86", Utilities.MultiArchPInvoke.IsRunningOnMono ? " (using Mono runtime)" : ""); Printer.WriteLineMessage("#q#- A less hateful version control system."); Printer.PushIndent(); Printer.WriteLineMessage("\n#b#Core version: {0}\n", Area.CoreVersion); foreach (var x in Area.ComponentVersions) { Printer.WriteLineMessage("{0}: #b#{1}", x.Item1, x.Item2); } Printer.PopIndent(); Printer.WriteLineMessage("\n#b#Plugins:\n"); Printer.PushIndent(); foreach (var plugin in PluginCache.Plugins) { Printer.WriteLineMessage("#b#{1}## ({2}) #q#{0}", Path.GetFileName(plugin.Assembly.Location), plugin.Attributes.Name, plugin.Assembly.GetName().Version); } Printer.PopIndent(); Printer.RestoreDefaults(); return; } if (args.Length == 0) { PrintAllOptions(args, parser, printerStream); printerStream.Flush(); Printer.PrintMessage("\n#e#Error## - missing command."); Printer.RestoreDefaults(); Environment.Exit(CommandLine.Parser.DefaultExitCodeFail); } // We will attempt to parse the commandline first object options = null; string invokedVerb = string.Empty; object invokedVerbInstance = null; object activatedPlugin = null; foreach (object pluginOptions in PluginOptions) { if (silentparser.ParseArguments(args, pluginOptions, (verb, success, subOptions) => { if (subOptions != null) { invokedVerb = verb; activatedPlugin = pluginOptions; } invokedVerbInstance = subOptions; })) { options = pluginOptions; break; } if (invokedVerb != string.Empty) { break; } } if (options == null) { if (invokedVerb != string.Empty && activatedPlugin != null) { // First, does the option object even support help? System.Reflection.MethodInfo helpOptionVerb = GetVerbHelpMethod(activatedPlugin); if (helpOptionVerb != null) { // We hit a verb, but the commandline parser is unhappy at us, re-run the parse parser.ParseArguments(args, activatedPlugin, (verb, success, subOptions) => { }); } else { if (invokedVerbInstance is VerbOptionBase) { printerStream.WriteLine(((VerbOptionBase)invokedVerbInstance).GetUsage()); } else { Printer.PrintMessage("Warning - verb #b#{0}##: command is malformed and cannot be parsed.", invokedVerb); Printer.PrintMessage("No help method defined on verb object.", invokedVerb); } } } else { PrintAllOptions(args, parser, printerStream); } printerStream.Flush(); Printer.RestoreDefaults(); Environment.Exit(CommandLine.Parser.DefaultExitCodeFail); } if (!string.IsNullOrEmpty((invokedVerbInstance as VerbOptionBase).Logfile)) { Printer.OpenLog((invokedVerbInstance as VerbOptionBase).Logfile); } Console.CancelKeyPress += Console_CancelKeyPress; try { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); // because lewis broke 'lg' on purpose if (args.Count() > 0 && args[0] == "lg" && invokedVerbInstance is Commands.LogVerbOptions) { ((Commands.LogVerbOptions)invokedVerbInstance).Jrunting = true; } int bmc = 1; bool bm = false; if (invokedVerbInstance is VerbOptionBase) { bm = ((VerbOptionBase)invokedVerbInstance).Benchmark; bmc = ((VerbOptionBase)invokedVerbInstance).BMC; } for (int i = 0; i < (bm ? bmc : 1); i++) { Commands.BaseCommand command = ((VerbOptionBase)invokedVerbInstance).GetCommand(); VerbOptionBase baseOptions = invokedVerbInstance as VerbOptionBase; if (baseOptions != null) { Printer.NoColours = baseOptions.NoColours; } bool result = command.Run(new System.IO.DirectoryInfo(workingDirectoryPath), invokedVerbInstance); if (!result) { printerStream.Flush(); Printer.RestoreDefaults(); Environment.Exit(2); } } if (bm) { Printer.PrintMessage("\nOperation took #b#{0}## ms.", sw.ElapsedMilliseconds); } printerStream.Flush(); Printer.RestoreDefaults(); return; } catch (Exception e) { printerStream.Flush(); System.Console.WriteLine("Error processing action:\n{0}", e.ToString()); Printer.RestoreDefaults(); Environment.Exit(20); } Printer.RestoreDefaults(); } catch { Environment.Exit(100); } return; }
static void Main(string[] args) { try { string workingDirectoryPath = Environment.CurrentDirectory; var printerStream = new Printer.PrinterStream(); VersionOptions initalOpts = new VersionOptions(); CommandLine.Parser parser = new CommandLine.Parser(new Action<ParserSettings>( (ParserSettings p) => { p.CaseSensitive = false; p.IgnoreUnknownArguments = false; p.HelpWriter = printerStream; p.MutuallyExclusive = true; })); if (parser.ParseArguments(args, initalOpts) && initalOpts.Version) { Printer.WriteLineMessage("#b#Versionr## v{0} #q#{1}{2}", System.Reflection.Assembly.GetCallingAssembly().GetName().Version, Utilities.MultiArchPInvoke.IsX64 ? "x64" : "x86", Utilities.MultiArchPInvoke.IsRunningOnMono ? " (using Mono runtime)" : ""); Printer.WriteLineMessage("#q# - A less hateful version control system."); Printer.PushIndent(); Printer.WriteLineMessage("\n#b#Core version: {0}\n", Area.CoreVersion); foreach (var x in Area.ComponentVersions) Printer.WriteLineMessage("{0}: #b#{1}", x.Item1, x.Item2); Printer.PopIndent(); Printer.RestoreDefaults(); return; } var options = new Options(); string invokedVerb = string.Empty; object invokedVerbInstance = null; if (!parser.ParseArguments(args, options, (verb, subOptions) => { invokedVerb = verb; invokedVerbInstance = subOptions; })) { printerStream.Flush(); Printer.RestoreDefaults(); Environment.Exit(CommandLine.Parser.DefaultExitCodeFail); } Dictionary<string, Commands.BaseCommand> commands = new Dictionary<string, Commands.BaseCommand>(); commands["init"] = new Commands.Init(); commands["commit"] = new Commands.Commit(); commands["status"] = new Commands.Status(); commands["record"] = new Commands.Record(); commands["checkout"] = new Commands.Checkout(); commands["branch"] = new Commands.Branch(); commands["server"] = new Commands.Server(); commands["push"] = new Commands.Push(); commands["merge"] = new Commands.Merge(); commands["log"] = new Commands.Log(); commands["lg"] = new Commands.Log(true); commands["remote"] = new Commands.Remote(); commands["behead"] = new Commands.Behead(); commands["viewdag"] = new Commands.ViewDAG(); commands["info"] = new Commands.Info(); commands["clone"] = new Commands.Clone(); commands["pull"] = new Commands.Pull(); commands["syncrecords"] = new Commands.SyncRecords(); commands["diff"] = new Commands.Diff(); commands["revert"] = new Commands.Revert(); commands["unrecord"] = new Commands.Unrecord(); commands["update"] = new Commands.Update(); commands["renamebranch"] = new Commands.RenameBranch(); commands["listbranch"] = new Commands.ListBranch(); commands["deletebranch"] = new Commands.DeleteBranch(); commands["stats"] = new Commands.Stats(); commands["expunge"] = new Commands.Expunge(); commands["mergeinfo"] = new Commands.MergeInfo(); commands["rebase"] = new Commands.Rebase(); commands["ahead"] = new Commands.Ahead(); commands["resolve"] = new Commands.Resolve(); Console.CancelKeyPress += Console_CancelKeyPress; Commands.BaseCommand command = null; Console.CancelKeyPress += Console_CancelKeyPress; if (!commands.TryGetValue(invokedVerb, out command)) { command = commands.Where(x => x.Key.Equals(invokedVerb, StringComparison.OrdinalIgnoreCase)).Select(x => x.Value).FirstOrDefault(); if (command == null) { printerStream.Flush(); System.Console.WriteLine("Couldn't invoke action: {0}", invokedVerb); Printer.RestoreDefaults(); Environment.Exit(10); } } try { VerbOptionBase baseOptions = invokedVerbInstance as VerbOptionBase; if (baseOptions != null) Printer.NoColours = baseOptions.NoColours; if (!command.Run(new System.IO.DirectoryInfo(workingDirectoryPath), invokedVerbInstance)) { Printer.RestoreDefaults(); printerStream.Flush(); Environment.Exit(2); } Printer.RestoreDefaults(); return; } catch (Exception e) { printerStream.Flush(); System.Console.WriteLine("Error processing action:\n{0}", e.ToString()); Printer.RestoreDefaults(); Environment.Exit(20); } Printer.RestoreDefaults(); } catch { Environment.Exit(100); } return; }