Esempio n. 1
0
 public static ArgumentsRule ExistingFilesOnly(
     this ArgumentsRule rule) =>
 rule.And(new ArgumentsRule(o => o.Arguments
                            .Where(filePath => !File.Exists(filePath) &&
                                   !Directory.Exists(filePath))
                            .Select(FileDoesNotExist)
                            .FirstOrDefault()));
Esempio n. 2
0
 public static Option Option(
     string aliases,
     string help,
     ArgumentsRule arguments = null) =>
 new Option(
     aliases.Split(
         new[] { '|', ' ' }, StringSplitOptions.RemoveEmptyEntries), help, arguments);
Esempio n. 3
0
 public static Command Command(
     string name,
     string help,
     ArgumentsRule arguments,
     bool treatUnmatchedTokensAsErrors,
     params Option[] options) =>
 new Command(name, help, options, arguments, treatUnmatchedTokensAsErrors);
Esempio n. 4
0
 public Option(
     string[] aliases,
     string help,
     ArgumentsRule arguments = null) :
     this(aliases, help, arguments, null)
 {
 }
Esempio n. 5
0
 public static Option Option(
     string aliases,
     string help,
     ArgumentsRule arguments = null,
     Func <AppliedOption, object> materialize = null) =>
 new Option(
     aliases.Split(
         new[] { '|', ' ' }, StringSplitOptions.RemoveEmptyEntries), help, arguments);
 public Command(
     string name,
     string help,
     Option[] options        = null,
     ArgumentsRule arguments = null,
     Func <AppliedOption, object> materialize = null) :
     base(new[] { name }, help, arguments, options, materialize)
 {
 }
        protected internal Option(
            string[] aliases,
            string help,
            ArgumentsRule arguments = null,
            Option[] options        = null,
            Func <AppliedOption, object> materialize = null)
        {
            if (aliases == null)
            {
                throw new ArgumentNullException(nameof(aliases));
            }

            if (!aliases.Any())
            {
                throw new ArgumentException("An option must have at least one alias.");
            }

            if (aliases.Any(string.IsNullOrWhiteSpace))
            {
                throw new ArgumentException("An option alias cannot be null, empty, or consist entirely of whitespace.");
            }

            foreach (var alias in aliases)
            {
                this.aliases.Add(alias.RemovePrefix());
            }

            HelpText = help;

            Name = aliases
                   .Select(a => a.RemovePrefix())
                   .OrderBy(a => a.Length)
                   .Last();

            this.materialize = materialize;

            if (options != null && options.Any())
            {
                foreach (var option in options)
                {
                    option.Parent = this;
                    DefinedOptions.Add(option);
                }
            }

            ArgumentsRule = arguments ?? Accept.NoArguments;

            if (options != null)
            {
                ArgumentsRule = Accept.ZeroOrMoreOf(options).And(ArgumentsRule);
            }

            AllowedValues = ArgumentsRule.AllowedValues;

            suggest = ArgumentsRule.Suggest;
        }
Esempio n. 8
0
 public Command(
     string name,
     string help,
     Option[] options                  = null,
     ArgumentsRule arguments           = null,
     bool treatUnmatchedTokensAsErrors = true) :
     base(new[] { name }, help, arguments, options)
 {
     TreatUnmatchedTokensAsErrors = treatUnmatchedTokensAsErrors;
 }
Esempio n. 9
0
 public static Command Command(
     string name,
     string help,
     ArgumentsRule arguments = null,
     Option[] options        = null,
     Func <AppliedOption, object> materialize = null) =>
 new Command(
     name,
     help,
     options,
     arguments,
     materialize);
Esempio n. 10
0
        internal static ArgumentsRule And(
            this ArgumentsRule rule,
            params ArgumentsRule[] rules)
        {
            rules = new[] { rule }.Concat(rules).ToArray();

            return(new ArgumentsRule(
                       option => rules.Select(r => r.Validate(option))
                       .FirstOrDefault(result => !string.IsNullOrWhiteSpace(result)),
                       rules.SelectMany(r => r.AllowedValues).Distinct().ToArray(),
                       suggest: result => rules.SelectMany(r => r.Suggest(result))));
        }
        public static ArgumentsRule MaterializeAs <T>(
            this ArgumentsRule rule,
            Func <AppliedOption, T> materialize)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }
            if (materialize == null)
            {
                throw new ArgumentNullException(nameof(materialize));
            }

            return(rule.With(materialize: o => materialize(o)));
        }
Esempio n. 12
0
        public static ArgumentsRule ExistingFilesOnly(
            this ArgumentsRule rule) =>
        rule.And(new ArgumentsRule(o =>
        {
            foreach (var filePath in o.Arguments)
            {
                if (!File.Exists(filePath) &&
                    !Directory.Exists(filePath))
                {
                    return($"File does not exist: {filePath}");
                }
            }

            return(null);
        }));
        public static ArgumentsRule And(
            this ArgumentsRule rule,
            ArgumentsRule rule2)
        {
            var rules = new[] { rule, rule2 };

            return(new ArgumentsRule(
                       validate: option => rules.Select(r => r.Validate(option))
                       .FirstOrDefault(result => !string.IsNullOrWhiteSpace(result)),
                       allowedValues: rules.SelectMany(r => r.AllowedValues)
                       .Distinct()
                       .ToArray(),
                       suggest: result => rules.SelectMany(r => r.Suggest(result)),
                       name: rule.Name ?? rule2.Name,
                       description: rule.Description ?? rule2.Description,
                       defaultValue: rule.GetDefaultValue ?? rule2.GetDefaultValue,
                       materialize: rule.Materializer ?? rule2.Materializer));
        }
Esempio n. 14
0
        public static ArgumentsRule With(
            this ArgumentsRule rule,
            string description         = null,
            string name                = null,
            Func <string> defaultValue = null)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }

            return(new ArgumentsRule(
                       validate: rule.Validate,
                       allowedValues: rule.AllowedValues,
                       defaultValue: defaultValue ??
                       (() => rule.DefaultValue),
                       description: name ?? rule.Name,
                       name: description ?? rule.Description));
        }
Esempio n. 15
0
        public static ArgumentsRule LegalFilePathsOnly(
            this ArgumentsRule rule) =>
        rule.And(new ArgumentsRule(o =>
        {
            foreach (var arg in o.Arguments)
            {
                try
                {
                    var fileInfo = new FileInfo(arg);
                }
                catch (NotSupportedException ex)
                {
                    return(ex.Message);
                }
                catch (ArgumentException ex)
                {
                    return(ex.Message);
                }
            }

            return(null);
        }));
        public static ArgumentsRule With(
            this ArgumentsRule rule,
            string description         = null,
            string name                = null,
            Func <string> defaultValue = null,
            Func <AppliedOption, object> materialize = null)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }

            return(new ArgumentsRule(
                       validate: rule.Validate,
                       allowedValues: rule.AllowedValues,
                       defaultValue: defaultValue ??
                       rule.GetDefaultValue,
                       name: name ?? rule.Name,
                       description: description ?? rule.Description,
                       suggest: rule.Suggest,
                       materialize: materialize ?? rule.Materialize));
        }
 public static ArgumentsRule And(
     this ArgumentsRule rule,
     Func <AppliedOption, string> error)
 {
     return(rule.And(new ArgumentsRule(error)));
 }
Esempio n. 18
0
 public static ArgumentsRule WithSuggestionsFrom(
     this ArgumentsRule rule,
     Func <string, IEnumerable <string> > suggest) =>
 rule.And(WithSuggestionsFrom(suggest));
Esempio n. 19
0
 public static Command Command(
     string name,
     string help,
     ArgumentsRule arguments,
     params Option[] options) =>
 new Command(name, help, options, arguments);
Esempio n. 20
0
 public static Option Option(
     string aliases,
     string help,
     ArgumentsRule arguments,
     Func <AppliedOption, object> materialize) =>
 Option(aliases, help, arguments);
Esempio n. 21
0
 public static ArgumentsRule WithSuggestionsFrom(
     this ArgumentsRule rule,
     params string[] values) =>
 rule.And(WithSuggestionsFrom(values));
Esempio n. 22
0
 public static Option Option(
     string aliases,
     string help,
     ArgumentsRule arguments = null,
     Func <AppliedOption, object> materialize = null) =>
 new Option(aliases.Split('|'), help, arguments, materialize: materialize);