Description of a command subcommand. These descriptions are lightweight compared to creating a command instance and are therefore suitable for catalogs of "known" commands without linking the command's implementation and creating a dummy instance of the command.
Пример #1
0
        /// <summary>
        /// Locates a single command by its user friendly name.
        /// </summary>
        /// <param name="name">Specifies the name of the command.</param>
        /// <returns>Returns the CommandRef containing the command's information.</returns>
        public CommandRef Get(String name)
        {
            CommandRef value = null;

            commands.TryGetValue(name, out value);
            return(value);
        }
Пример #2
0
        /// <summary>
        /// Create and loads the command name into the command catalog.
        /// </summary>
        /// <param name="commandName">Specifies the command name to load.</param>
        /// <param name="commandHelp">Specifies the command's website for faster reference.</param>
        public void Load(String commandName, String commandHelp)
        {
            TextBuiltin clazz;

            Type commandType = Type.GetType(commandName);

            if (commandType == null)
            {
                return;
            }

            clazz = Activator.CreateInstance(commandType) as TextBuiltin;
            if (clazz == null)
            {
                return;
            }

            int    index   = clazz.ToString().LastIndexOf(".");
            string cmdName = clazz.ToString().Substring(index + 1).ToLower();

            clazz.setCommandName(cmdName);
            clazz.setCommandHelp(commandHelp);

            CommandRef cr = new CommandRef(clazz);

            if (cr != null)
            {
                commands.Add(cr.getName(), cr);
            }
        }
Пример #3
0
        /// <summary>
        /// Create and loads the command name into the command catalog.
        /// </summary>
        /// <param name="commandName">Specifies the command name to load.</param>
        /// <param name="commandHelp">Specifies the command's website for faster reference.</param>
        public void Load(String commandName, String commandHelp)
        {
            TextBuiltin clazz;

            Type commandType = Type.GetType(commandName);
            if (commandType == null)
                return;

            clazz = Activator.CreateInstance(commandType) as TextBuiltin;
            if (clazz == null)
                return;

            int index = clazz.ToString().LastIndexOf(".");
            string cmdName = clazz.ToString().Substring(index + 1).ToLower();
            clazz.setCommandName(cmdName);
            clazz.setCommandHelp(commandHelp);

            CommandRef cr = new CommandRef(clazz);
            if (cr != null)
                commands.Add(cr.getName(), cr);
        }
Пример #4
0
        /// <summary>
        /// Execute the command line
        /// </summary>
        /// <param name="argv"></param>
        private static void execute(string[] argv)
        {
            if (argv.Count() == 0)
            {
                ShowHelp();
            }
            else if (!argv[0].StartsWith("--") && !argv[0].StartsWith("-"))
            {
                CommandCatalog catalog    = new CommandCatalog();
                CommandRef     subcommand = catalog.Get(argv[0]);
                string         gitdir     = null;

                if (subcommand != null)
                {
                    TextBuiltin         cmd  = subcommand.Create();
                    List <String>       args = argv.ToList();
                    GitSharp.Repository repo = null;

                    try
                    {
                        for (int x = 0; x < args.Count; x++)
                        {
                            if (args[x].IndexOf("--git-dir=") > -1)
                            {
                                if (args[x].Length > 10)
                                {
                                    gitdir = args[x].Substring(10);
                                    args.RemoveAt(x);
                                    break;
                                }
                            }
                        }
                    }
                    catch (ArgumentException)
                    {
                        if (Git.DefaultOutputStream != null)
                        {
                            Git.DefaultOutputStream.WriteLine("error: can't find git directory");
                            Git.DefaultOutputStream.Flush();
                        }
                        Exit(1);
                    }

                    if (cmd.RequiresRepository)
                    {
                        if (gitdir == null)
                        {
                            gitdir = Commands.AbstractCommand.FindGitDirectory(null, true, false);
                            if (gitdir == null)
                            {
                                Console.Error.WriteLine("fatal: Not a git repository (or any of the parent directories): .git");
                                Exit(0);
                            }
                        }

                        repo = new GitSharp.Repository(gitdir);
                        cmd.Init(repo, gitdir);
                    }
                    else
                    {
                        cmd.Init(null, gitdir);
                    }

                    try
                    {
                        // Remove the subcommand from the command line
                        args.RemoveAt(0);
                        cmd.Execute(args.ToArray());
                    }
                    finally
                    {
                        if (Git.DefaultOutputStream != null)
                        {
                            Git.DefaultOutputStream.Flush();
                        }

                        if (repo != null)
                        {
                            repo.Close();
                        }
                    }
                }
                else
                {
                    // List all available commands starting with argv[0] if the command
                    // specified does not exist.
                    // If no commands exist starting with argv[0], show the help screen.
                    if (!ShowCommandMatches(argv[0]))
                    {
                        ShowHelp();
                    }
                }
            }
            else
            {
                // If the first argument in the command line is an option (denoted by starting with - or --),
                // no subcommand has been specified in the command line.
                try
                {
                    options.Parse(argv, out arguments);
                }
                catch (OptionException err)
                {
                    if (arguments.Count > 0)
                    {
                        Console.Error.WriteLine("fatal: " + err.Message);
                        Exit(1);
                    }
                }
            }
            Exit(0);
        }