Наследование: MainCommandGroupBase
Пример #1
0
        public static void Main([NotNull] string[] args)
        {
            Assert.ArgumentNotNull(args, "args");

              CoreApp.InitializeLogging();

              CoreApp.LogMainInfo();

              Analytics.Start();

              var filteredArgs = args.ToList();
              var query = GetQueryAndFilterArgs(filteredArgs);
              var wait = GetWaitAndFilterArgs(filteredArgs);

              var parser = new Parser(with => with.HelpWriter = Console.Error);
              Assert.IsNotNull(parser, "parser");

              var options = new MainCommandGroup();
              EnsureAutoCompeteForCommands(options);
              if (!parser.ParseArguments(filteredArgs.ToArray(), options, delegate { }))
              {
            Console.WriteLine("Note, commands provide output when work is done i.e. without any progress indication.");
            Console.WriteLine("\r\n  --query\t   When specified, allows returning only part of any command's output");
            Console.WriteLine("\r\n  --data\t   When specified, allows returning only 'data' part of any command's output");
            Console.WriteLine("\r\n  --wait\t   When specified, waits for keyboard input before terminating");

            Environment.Exit(Parser.DefaultExitCodeFail);
              }

              var result = options.SelectedCommand.Execute();

              result = QueryResult(result, query);
              if (result == null)
              {
            return;
              }

              var serializer = new JsonSerializer
              {
            NullValueHandling = NullValueHandling.Ignore,
            Formatting = Formatting.Indented,
              };

              serializer.Converters.Add(new DirectoryInfoConverter());

              var writer = Console.Out;
              serializer.Serialize(writer, result);

              if (wait)
              {
            Console.ReadKey();
              }
        }
Пример #2
0
        private static void EnsureAutoCompeteForCommands(MainCommandGroup options)
        {
            foreach (var propertyInfo in options.GetType().GetProperties())
              {
            if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType))
            {
              var verb = propertyInfo.GetCustomAttributes().OfType<VerbOptionAttribute>().FirstOrDefault();
              if (verb == null)
              {
            continue;
              }

              var command = verb.LongName;
              if (File.Exists(command))
              {
            continue;
              }

              File.Create(command).Close();
            }
              }
        }