예제 #1
0
 public int Run()
 {
     try
     {
         var a = new Args(this.Arguments);
         if (OnException != null)
         {
             a.OnException = (e) =>
             {
                 return(this.OnException(e));
             };
         }
         var entryAssembly = Assembly.GetCallingAssembly();
         var programName   = this.Application.GetType().Assembly.GetName().Name;
         var rootCommand   = ObjectCommand.Create(programName, ObjectProvider.Create(new Starter(this.Application, a)));
         return(rootCommand.Invoke(a));
     }
     catch (ParseError ex)
     {
         Console.Error.WriteLine(ex.Message);
         return(-1);
     }
     catch (Exception ex)
     {
         Console.Error.WriteLine(ex);
         return(-1);
     }
 }
예제 #2
0
        public static ICommand Create(string programName, IObjectProvider objectProvider)
        {
            var c = new ObjectCommand(null, programName);

            c.CommandSource = new ObjectCommandSource(c, objectProvider);
            return(c.AddHelp());
        }
예제 #3
0
        static IEnumerable <ICommand> GetCommands(ICommand parent, IObjectProvider containingObject, MemberInfo m, IEnumerable <IOption> inheritedOptions)
        {
            if (m.GetCustomAttribute <ModuleAttribute>() != null)
            {
                return(GetCommands(parent, ObjectProvider.ResolveNow(containingObject, m), inheritedOptions));
            }

            return(ObjectCommand.Create(parent, m, containingObject).ToEnumerable());
        }
예제 #4
0
        public static ICommand AddHelp(this ObjectCommand command)
        {
            var help = new HelpOption();

            command.CommandSource = command.CommandSource.Concat(new ObjectCommandSource(
                                                                     null,
                                                                     ObjectProvider.Create(help.GetType(), () => help)
                                                                     ));
            help.Command = command;
            return(command);
        }
예제 #5
0
        public static Maybe <ICommand> Create(ICommand parent, MemberInfo member, IObjectProvider containingObject)
        {
            var getter = member.GetGetter(containingObject);

            return(member.GetCustomAttribute <CommandAttribute>().ToMaybe()
                   .Select(command =>
            {
                var name = Util.CSharpIdentifierToLongOption(member.Name);
                var c = new ObjectCommand(parent, name);
                c.CommandSource = new ObjectCommandSource(c, getter);
                return (ICommand)c.AddHelp();
            }));
        }