Example #1
0
 public string GetUsage(string verb)
 {
     foreach (var x in GetType().GetProperties())
     {
         if (x.PropertyType.IsSubclassOf(typeof(VerbOptionBase)))
         {
             VerbOptionBase vob = x.GetValue(this) as VerbOptionBase;
             if (vob != null && verb == vob.Verb)
             {
                 return(vob.GetUsage());
             }
         }
     }
     return(GetUsage());
 }
Example #2
0
        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;
        }