Пример #1
0
        CmdlineParseItem AddParameterArray(string name, string description, bool mandatory)
        {
            var item = new CmdlineParseItem(this, CmdlineParseItemType.parameterArray,
                                            name, null, null, description, mandatory: mandatory, globalFlagAction: null);

            items.Add(item);

            return(item);
        }
Пример #2
0
        /// <summary>
        /// shortName or longName and valueName can be null
        /// </summary>
        CmdlineParseItem AddFlag(string shortName, string longName, string description, string valueName, bool mandatory, Action <CmdlineParseItem> globalFlagAction, bool globalFlagActionNested)
        {
            var item = new CmdlineParseItem(this, CmdlineParseItemType.flag,
                                            shortName, longName, valueName, description, mandatory, globalFlagAction, globalFlagActionNested);

            items.Add(item);

            return(item);
        }
Пример #3
0
        /// <summary>
        /// add a command item to this parser
        /// </summary>
        /// <param name="name">name of the command</param>
        /// <param name="description">description of the command ( for the usage )</param>
        /// <param name="builder">an optional builder to create a subparser from this command</param>
        public CmdlineParseItem AddCommand(string name, string description, Action <CmdlineParser> builder = null)
        {
            var parser = new CmdlineParser(this, null, unescapeArguments);

            var cmd = new CmdlineParseItem(parser, CmdlineParseItemType.command,
                                           name, null, null, description, mandatory: false, globalFlagAction: null);

            parser.Command = cmd;

            items.Add(cmd);

            if (builder != null)
            {
                builder(parser);
            }

            return(cmd);
        }
Пример #4
0
        IEnumerable <Action> InternalRun(List <CmdlineArgument> args)
        {
            showCompletion = Environment.GetEnvironmentVariable("SHOW_COMPLETIONS").Eval((e) => e != null && (e == "1" || e == "2"));

            CmdlineParseItem cmdToRun = null;

            var missingCommand = false;
            var missingFlag    = false;

            #region flags
            if (AllFlags.Any())
            {
                foreach (var(arg, argIdx) in args.WithIndex())
                {
                    if (arg.Matched)
                    {
                        continue;
                    }

                    var availFlags = AllFlags.Where(r => !r.Matches);
                    if (!availFlags.Any())
                    {
                        break;                    // all flags consumed
                    }
                    CmdlineParseItem qFlag = null;

                    foreach (var flag in availFlags)
                    {
                        #region short flag
                        if (flag.HasShortName)
                        {
                            if (flag.HasValueName)
                            {
                                if (arg.Argument == flag.ShortFlag)
                                {
                                    if (argIdx < args.Count - 1)
                                    {
                                        var valArg = args[argIdx + 1];
                                        valArg.MatchedItem = flag;
                                        flag.SetValue(valArg);
                                        qFlag = flag;
                                        break;
                                    }
                                }
                                else if (arg.Argument.StartsWith($"{flag.ShortFlag}="))
                                {
                                    flag.SetValue(arg, arg.Argument.Substring($"{flag.ShortFlag}=".Length));
                                    qFlag = flag;
                                    break;
                                }
                                else if (flag.ShortFlag.StartsWith(arg.Argument))
                                {
                                    PrintCompletions(new[] { $"{flag.ShortFlag}=" });
                                }
                            }
                            else
                            {
                                if (arg.Argument == flag.ShortFlag)
                                {
                                    qFlag = flag;
                                    break;
                                }
                                else if (flag.ShortFlag.StartsWith(arg.Argument))
                                {
                                    PrintCompletions(new[] { flag.ShortFlag });
                                }
                            }
                        }
                        #endregion

                        #region long flag
                        if (flag.HasLongName)
                        {
                            if (flag.HasValueName)
                            {
                                if (arg.Argument == flag.LongFlag)
                                {
                                    if (argIdx < args.Count - 1)
                                    {
                                        var valArg = args[argIdx + 1];
                                        valArg.MatchedItem = flag;
                                        flag.SetValue(valArg);
                                        qFlag = flag;
                                        break;
                                    }
                                }
                                else if (arg.Argument.StartsWith($"{flag.LongFlag}="))
                                {
                                    flag.SetValue(arg, arg.Argument.Substring($"{flag.LongFlag}=".Length));
                                    qFlag = flag;
                                    break;
                                }
                                else if (flag.LongFlag.StartsWith(arg.Argument))
                                {
                                    PrintCompletions(new[] { $"{flag.LongFlag}=" });
                                }
                            }
                            else
                            {
                                if (arg.Argument == flag.LongFlag)
                                {
                                    qFlag = flag;
                                    break;
                                }
                                else if (flag.LongFlag.StartsWith(arg.Argument))
                                {
                                    PrintCompletions(new[] { flag.LongFlag });
                                }
                            }
                        }
                        #endregion
                    }

                    if (qFlag != null)
                    {
                        qFlag.Match(this, arg);
                        if (!qFlag.GlobalFlagActionNested && qFlag.GlobalFlagAction != null)
                        {
                            qFlag.GlobalFlagAction(qFlag);
                            qFlag.GlobalFlagActionExecuted = true;
                        }
                    }
                }

                if (!showCompletion)
                {
                    var qMandatoryMissing = Flags.FirstOrDefault(r => r.Mandatory && !r.Matches);
                    if (qMandatoryMissing != null)
                    {
                        ErrorColor();
                        System.Console.WriteLine($"missing mandatory flag [{qMandatoryMissing.ShortLongFlag}]");
                        ResetColors();
                        PrintUsage();
                        missingFlag = true;
                    }
                }
            }
            #endregion

            #region commands
            if (Commands.Any())
            {
                var arg = args.FirstOrDefault(w => !w.Matched);

                if (arg != null)
                {
                    var qcmd = Commands.FirstOrDefault(w => w.ShortName == arg.Argument);
                    // if not found valid command checks global flags
                    if (qcmd == null)
                    {
                        missingCommand = true;

                        if (showCompletion)
                        {
                            PrintCompletions(Commands.Select(w => w.ShortName).Where(r => r.StartsWith(arg.Argument) && r != arg.Argument));
                        }
                    }
                    else
                    {
                        qcmd.Match(this, arg);
                        cmdToRun = qcmd;
                    }
                }
                else
                {
                    missingCommand = true;

                    if (showCompletion)
                    {
                        PrintCompletions(Commands.Select(w => w.ShortName));
                    }
                }
            }
            #endregion

            CmdlineParseItem missingParameter = null;

            #region parameters
            if (ParametersOrArray.Any())
            {
                foreach (var param in Parameters)
                {
                    var arg = args.FirstOrDefault(r => !r.Matched);

                    if (arg == null)
                    {
                        if (showCompletion)
                        {
                            if (param.onCompletion != null && !missingCommand)
                            {
                                PrintCompletions(param.onCompletion(""));
                            }
                        }
                        else
                        {
                            if (param.Mandatory)
                            {
                                missingParameter = param;
                            }
                            break;
                        }
                    }
                    else
                    {
                        var skipCompletion = !showCompletion;

                        if (showCompletion)
                        {
                            if (param.onCompletion != null && !missingCommand)
                            {
                                var completions = param.onCompletion(arg.Argument).Where(r => r != arg.Argument);
                                if (completions.Count() > 0)
                                {
                                    PrintCompletions(completions);
                                }
                                else
                                {
                                    skipCompletion = true;
                                }
                            }
                        }

                        if (skipCompletion)
                        {
                            param.Match(this, arg);
                            param.SetValue(arg);
                        }
                    }
                }

                var parr = ParameterArrays.FirstOrDefault();

                if (parr != null)
                {
                    var parrArgs = new List <CmdlineArgument>();
                    while (true)
                    {
                        var arg = args.FirstOrDefault(r => !r.Matched);
                        if (arg == null)
                        {
                            if (showCompletion)
                            {
                                if (parr.onCompletion != null && !missingCommand)
                                {
                                    PrintCompletions(parr.onCompletion(""));
                                }
                            }
                            else
                            {
                                if (parr.Mandatory && parrArgs.Count == 0)
                                {
                                    missingParameter = parr;
                                }
                            }
                            break;
                        }
                        else
                        {
                            var skipCompletion = !showCompletion || parr.onCompletion == null;

                            if (showCompletion)
                            {
                                if (parr.onCompletion != null && !missingCommand)
                                {
                                    var completions = parr.onCompletion(arg.Argument).Where(r => r != arg.Argument);
                                    if (completions.Count() > 0)
                                    {
                                        PrintCompletions(completions);
                                        break;
                                    }
                                    else
                                    {
                                        skipCompletion = true;
                                    }
                                }
                            }

                            if (skipCompletion)
                            {
                                parr.Match(this, arg);
                                parrArgs.Add(arg);
                            }
                        }
                    }
                    parr.SetValues(parrArgs);
                }
            }
            #endregion

            var qglobal = AllFlags.Where(r => r.IsGlobal && r.GlobalFlagActionNested && r.Matches).ToList();

            if (!showCompletion && qglobal.Count == 0 && missingCommand)
            {
                ErrorColor();
                System.Console.WriteLine($"missing command");
                ResetColors();
                PrintUsage();
                yield break;
            }

            if (!showCompletion && missingParameter != null)
            {
                ErrorColor();
                System.Console.WriteLine($"missing required parameter [{missingParameter.ShortName}]");
                ResetColors();
                PrintUsage();
                yield break;
            }

            if (!showCompletion && onCmdlineMatch != null && qglobal.Count == 0 && !missingFlag)
            {
                yield return(onCmdlineMatch);
            }

            if (cmdToRun != null)
            {
                var qGlobalToremove = new List <CmdlineParseItem>();
                foreach (var x in qglobal)
                {
                    if (!x.GlobalFlagActionNested)
                    {
                        qGlobalToremove.Add(x);
                    }
                    else
                    {
                        x.Unmatch();
                    }
                }
                foreach (var x in qGlobalToremove)
                {
                    qglobal.Remove(x);
                }

                if (!missingFlag)
                {
                    foreach (var x in cmdToRun.Parser.InternalRun(args))
                    {
                        yield return(x);
                    }
                }
                yield break;
            }
            if (cmdToRun == null && qglobal.Count > 0)
            {
                foreach (var x in qglobal.Where(r => !r.GlobalFlagActionExecuted))
                {
                    x.GlobalFlagAction(x);
                }
                yield break;
            }
        }