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); } }
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()); }
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); }
public static IEnumerable <IOption> GetOptions(IObjectProvider container, MemberInfo member) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (member == null) { throw new ArgumentNullException(nameof(member)); } if (!(member is FieldInfo || member is PropertyInfo)) { return(noOptions); } if (member.GetCustomAttribute <ModuleAttribute>() != null) { return(Option.GetOptions(ObjectProvider.ResolveNow(container, member))); } var usage = member.GetUsage(); if (!usage.HasValue) { return(noOptions); } var command = member.GetCustomAttribute <CommandAttribute>().ToMaybe(); if (command.HasValue) { return(noOptions); } if (member is FieldInfo) { return(new[] { (IOption) new FieldOption(container, (FieldInfo)member) }); } else if (member is PropertyInfo) { return(new[] { (IOption) new PropertyOption(container, (PropertyInfo)member) }); } throw new NotSupportedException(String.Format("{0} of type {1} is not supported.", member, member.GetType())); }
public static Maybe <ICommand> Create(ICommand parent, IObjectProvider containingObject, MethodInfo method, IEnumerable <IOption> inheritedOptions) { if (inheritedOptions == null) { throw new ArgumentNullException(nameof(inheritedOptions)); } return(method .GetUsage() .Select(usage => { var help = new HelpOption(); var helpCommand = new ObjectCommandSource(null, ObjectProvider.Create(help)); var c = new MethodCommand(parent, containingObject, method, helpCommand.Options.Concat(inheritedOptions)); help.Command = c; return (ICommand)c; })); }