Ejemplo n.º 1
0
 private NodeSequence Parse(params string[] arg)
 {
     Parser parser = new Parser();
     return parser.Parse(string.Join(" ", arg));
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            /*
             *  simple usage
             */

            // set up argument parser
            ArgumentParser<ExecuteCommandBase> argParser = new ArgumentParser<ExecuteCommandBase>(typeof(ExecuteCommandUsingName), typeof(ExecuteCommandUsingPath));
            ExecuteCommandBase paramSet;
            if (argParser.TryParse(args, out paramSet))
            {
                paramSet.Execute();
            }

            Console.ReadLine();

            /*
            *  advanced usage
            */

            // create parameter set collection from types
            ParameterSetCollection sets = ParameterSetCollection.FromTypes(typeof(ExecuteCommandUsingName), typeof(ExecuteCommandUsingPath));

            // parse the command line arguments
            Parser parser = new Parser();
            NodeSequence nodes = parser.Parse(string.Join(" ", args));

            // resolve parameter set against the parsed node set
            ResolveResult result = sets.Resolve(new ParameterSetBinder(),
                                                DefaultActivator.Instance,
                                                new IntransigentTypeConverter(),
                                                CultureInfo.InvariantCulture,
                                                nodes);
            if (result.IsMatch)
            {
                paramSet = (ExecuteCommandBase)result.BestMatch.Object;
                paramSet.Execute();
            }
            else
            {
                ErrorWriter errorWriter = new ErrorWriter();
                errorWriter.Write(new ConsoleWriter(Console.Error), result);

                HelpWriter helpWriter = new HelpWriter();
                helpWriter.Write(new ConsoleWriter(Console.Out), sets, HelpLevel.Parameters);
            }
        }
Ejemplo n.º 3
0
 private NodeSequence Parse(string arg)
 {
     Parser parser = new Parser();
     return parser.Parse(arg);
 }
Ejemplo n.º 4
0
 private NodeSequence Parse(params string[] arg)
 {
     //Tokenizer tok = new Tokenizer()
     //Token[] tokens = tok.Tokenize(arg).ToArray();
     Parser parser = new Parser();
     return parser.Parse(string.Join(" ", arg));
 }
Ejemplo n.º 5
0
        private static void SetDefaultValues(ITypeConverter typeConverter, CultureInfo cultureInfo, BuildContext ctx)
        {
            // set default values
            foreach (Parameter parameter in ctx.ParameterSet)
            {
                var attr = parameter.GetAttribute<DefaultValueAttribute>();

                if (attr == null)
                    continue;

                string strVal = attr.Value as string;
                if (strVal != null)
                {
                    if (parameter.Property.PropertyType != typeof(string))
                    {
                        Parser parser = new Parser();
                        NodeSequence seq = parser.Parse(strVal);
                        using (ValueBuilder valueBuilder = new ValueBuilder())
                        {
                            object value;
                            if (valueBuilder.Build(parameter.Property.PropertyType, seq[0], out value))
                            {
                                parameter.Property.SetValue(ctx.Instance, value, null);
                            }
                            else
                            {
                                string message = string.Format(Exceptions.FailedToParseDefaultValue,
                                                               strVal,
                                                               parameter.Property.PropertyType.FullName);

                                throw new ParameterDefinitionException(parameter.Property, message);
                            }
                        }
                    }
                    else
                    {
                        parameter.Property.SetValue(ctx.Instance, strVal, null);
                    }
                }
                else
                {
                    object value = attr.Value;
                    if (value == null)
                    {
                        if (!parameter.Property.PropertyType.IsValueType)
                        {
                            parameter.Property.SetValue(ctx.Instance, value, null);
                        }
                        else
                        {
                            string message = string.Format(Exceptions.FailedToConvertDefaultValue,
                                                           "NULL",
                                                           "",
                                                           parameter.Property.PropertyType.FullName);
                            throw new ParameterDefinitionException(parameter.Property, message);
                        }
                    }
                    else
                    {
                        IEnumerable<Exception> exceptions;
                        if (typeConverter.TryConvertType(cultureInfo, parameter.Property.PropertyType, ref value, out exceptions))
                        {
                            parameter.Property.SetValue(ctx.Instance, value, null);
                        }
                        else
                        {
                            string message = string.Format(Exceptions.FailedToConvertDefaultValue,
                                                           value,
                                                           value.GetType().FullName,
                                                           parameter.Property.PropertyType.FullName);

                            throw new ParameterDefinitionException(parameter.Property, message);
                        }
                    }
                }
            }
        }