Ejemplo n.º 1
0
        public CmdLineUsageWriter(CmdLineDefinition definition)
        {
            myDefinition = definition;

            myTextWriter  = definition.Configuration.UsageTextWriter;
            myErrorAction = definition.Configuration.ErrorAction;
        }
        public void BasicsWithoutSubcommand()
        {
            var theConsoleOutput = new StringWriter();

            var theCmdLineDefinition = new CmdLineDefinition {
                HasDifferentiatedSubcommands = false,
                Configuration = new CmdLineConfiguration {
                    UsageTextWriter = theConsoleOutput,
                    ErrorAction     = () => { }
                }
            };

            Assert.Throws <ArgumentNullException>(() => theCmdLineDefinition.Try(null !));

            Assert.Throws <ArgumentNullException>(() => theCmdLineDefinition.Try(new string[] { null ! }));
Ejemplo n.º 3
0
        internal override int Execute(IEnumerable <CmdLineToken> argTokens, CmdLineDefinition definition)
        {
            var  theOptions     = new TOptions();
            Type theOptionsType = typeof(TOptions);

            foreach (CmdLineToken theArgToken in argTokens)
            {
                if (theArgToken.Kind != CmdLineTokenKind.Subcommand)
                {
                    // Option oder Parameter
                    string thePropertyName = theArgToken.Name.Replace("-", string.Empty); // de-kebap-style

                    // Spezifische (Attribute) PropertyInfo finden
                    PropertyInfo thePropertyInfo;

                    if (theArgToken.Kind == CmdLineTokenKind.Option)
                    {
                        thePropertyInfo =
                            theOptionsType
                            .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                            .SingleOrDefault(
                                pi => pi.GetCustomAttribute <CmdLineOptionAttribute>() != null &&
                                pi.GetCustomAttribute <CmdLineOptionAttribute>().Letter.ToString() == thePropertyName
                                );
                    }
                    else
                    {
                        // Parameter
                        thePropertyInfo =
                            theOptionsType
                            .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                            .SingleOrDefault(
                                pi => pi.GetCustomAttribute <CmdLineParameterAttribute>() != null &&
                                pi.GetCustomAttribute <CmdLineParameterAttribute>().Name.ToLower() == thePropertyName
                                );
                    }

                    if (thePropertyInfo == null)
                    {
                        // Property mit genau dem passenden Namen (case-insensitive) finden
                        thePropertyInfo = theOptionsType.GetProperty(
                            thePropertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase
                            );
                    }

                    if (thePropertyInfo != null)
                    {
                        // Property gefunden, Wert setzen
                        thePropertyInfo.SetMethod.Invoke(theOptions, new[] { theArgToken.Value });
                    }
                    else
                    {
                        throw new InvalidOperationException(
                                  $"No property \"{thePropertyName}\" found on type {theOptionsType.FullName}."
                                  );
                    }
                }
            }

            return(CmdLineFunc.Invoke(theOptions, definition));
        }
 public CmdLineDefinitionExecutor(CmdLineDefinition definition, string[] args)
 {
     myDefinition = definition;
     myArgs       = args;
 }
Ejemplo n.º 5
0
 public CmdLineTokenizer(CmdLineDefinition definition, CmdLineUsageWriter usageWriter)
 {
     myDefinition  = definition;
     myUsageWriter = usageWriter;
 }