コード例 #1
0
        public void Interpret(Type baseContext)
        {
            _topContextAttribute = baseContext.GetCustomAttribute(typeof(CmdContextAttribute)) as CmdContextAttribute;
            if (_topContextAttribute is null)
            {
                throw new InvalidCLIConfigurationException("The Type provided has no CmdContextAttribute");
            }

            var contextInterpreter =
                new ContextInterpreter(this, _topContextAttribute)
            {
                UnderlyingContextAttribute = { UnderlyingType = baseContext.GetTypeInfo() }
            };

            contextInterpreter.UnderlyingContextAttribute.Load();
            if (Args.Length > 0)
            {
                if (contextInterpreter.IsParameterEqual(Options.HexOption, Args[0]))
                {
                    HexArgumentEncoding.ArgumentsFromHex(Args[1], out List <string> newArgs);
                    Args = newArgs.ToArray();
                    Interpret(baseContext);

                    return;
                }

                if (contextInterpreter.IsParameterEqual(Options.InteractiveOption, Args[0]))
                {
                    contextInterpreter.IncreaseOffset();
                    contextInterpreter.InteractiveInterpreter(contextInterpreter.Offset < Args.Length);
                }
            }

            try {
                contextInterpreter.Interpret();
            }
            catch (CLIUsageException e) {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException);
                }

                throw;
            }
        }
コード例 #2
0
        /// <summary>
        ///  Writes the help for a context to a textwriter
        /// </summary>
        /// <param name="context">The context to provide help</param>
        /// <param name="width">The width of the console</param>
        /// <param name="indent">The indent to use for splitted lines</param>
        /// <param name="tw">The textwriter to output to (defaults to <see cref="Console.Out" /></param>
        private static void ContextHelp([NotNull] CmdContextAttribute context, int width, int indent = 3, TextWriter tw = null)
        {
            tw = tw ?? Console.Out;
            context.Load();
            tw.Write(CommandlineMethods.PadCentered(context.Name, width));
            if (context.LongDescription is null)
            {
                if (!(context.Description is null))
                {
                    CommandlineMethods.PrintWithPotentialIndent(context.Description, width, indent, tw);
                }
            }
            else
            {
                foreach (string s in context.LongDescription)
                {
                    CommandlineMethods.PrintWithPotentialIndent(s, width, 0, tw);
                }
            }

            if (context.SubCtx.Count != 0)
            {
                tw.Write(CommandlineMethods.PadCentered("Contexts", width));
                foreach (CmdContextAttribute subCtx in context.SubCtx)
                {
                    CommandlineMethods.PrintWithPotentialIndent(
                        $"{(subCtx.ShortForm is null ? "" : "-" + subCtx.ShortForm + " | ")}--{subCtx.Name}{(subCtx.Description is null ? "" : $": {subCtx.Description}")}",
                        width, indent, tw);
                }
            }

            if (context.CtxActions.Count != 0)
            {
                tw.Write(CommandlineMethods.PadCentered("Actions", width));
                foreach (CmdActionAttribute action in context.CtxActions)
                {
                    CommandlineMethods.PrintWithPotentialIndent(
                        $"{(action.ShortForm is null ? "" : $"-{action.ShortForm} | ")}--{action.Name}{(action.Description is null ? "" : $": {action.Description}")}",
                        width, indent, tw);
                }
            }
コード例 #3
0
 /// <summary>
 ///  Prints help for a context
 /// </summary>
 /// <param name="context">The context to print help</param>
 /// <param name="interpreter">The interpreter to use</param>
 public static void PrintContextHelp(CmdContextAttribute context, BaseInterpreter interpreter) =>
 ContextHelp(context, Console.WindowWidth, interpreter.TopInterpreter.Options.DefaultIndent);
コード例 #4
0
 internal ContextInterpreter([NotNull] CommandlineOptionInterpreter top,
                             [NotNull] CmdContextAttribute underlyingContextAttribute, int offset = 0) :
     base(top, underlyingContextAttribute.Name, offset) =>