public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); if (result.Succeeded) { string path = arg.Value; if (!Path.IsPathFullyQualified(path)) { try { path = Path.Combine(GeneralParameters.Instance.WorkingFolder, path); } catch (Exception ex) { result.Append(new CommandResult(false, "Invalid path:", ex)); } } if (result.Succeeded) { if (!FileSupport.IsFolderReadable(path)) { return(new CommandResult(false, $"Folder '{path}' does not exist or is not readable")); } arg.ResolvedValue = path; } } return(result); }
public CommandArg Resolve(CommandArg arg) { var result = Validate(arg); if (!result.Succeeded) { throw new ArgumentException(result.ToString()); } return(arg); }
public virtual CommandResult Validate(CommandArg arg) { if (String.IsNullOrEmpty(arg.Name) && arg.Mnemonic == '\0') { return(new CommandResult(false, $"Invalid argument in command line")); } else { return(new CommandResult()); } }
public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); if (result.Succeeded) { if (arg.Value.Length > _maxLength || !Regex.IsMatch(arg.Value, _namePattern)) { result.Append(new CommandResult(false, $"{arg.Name} name is invalid")); } } return(result); }
public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); if (result.Succeeded) { if (Uri.TryCreate(arg.Value.ToString(), UriKind.Absolute, out Uri uri)) { arg.ResolvedValue = uri; return(result); } } return(new CommandResult(false, $"'{arg.Value}' is not a valid URL")); }
public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); try { arg.ResolvedValue = arg.Value.GetObject <T>(); } catch (Exception ex) { result.Append(new CommandResult(false, $"Unable to construct {typeof(T).Name}", ex)); } return(result); }
public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); if (result.Succeeded) { if (Uri.TryCreate("tcp://" + arg.Value, UriKind.Absolute, out Uri uri)) { if (uri.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped) == arg.Value) { return(result); } } } return(new CommandResult(false, $"'{arg.Value}' is not a valid URL")); }
public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); try { if (result.Succeeded) { arg.ResolvedValue = arg.Value.GetValue <T>(); } } catch (Exception ex) { result.Append(new CommandResult(false, $"Invalid value for {arg.Name}", ex)); } return(result); }
public void Register(CommandLineProto proto) { if (proto != null) { CommandArg name = proto.GetPositional(0); if (name != null) { Register(name.Value, proto); } else { throw new ArgumentException($"Command cannot be registered"); } } else { throw new ArgumentNullException("Proto argument cannot be null"); } }
public override CommandResult Validate(CommandArg arg) { CommandResult result = base.Validate(arg); string path = arg.Value; if (HasDefaultExt && Path.GetExtension(path) == "") { path += DefaultExt; } if (!Path.IsPathFullyQualified(path)) { try { path = Path.Combine(GeneralParameters.Instance.WorkingFolder, path); } catch (Exception ex) { result.Append(new CommandResult(false, "Invalid path:", ex)); } } if (result.Succeeded) { if (Exists && !File.Exists(path)) { return(new CommandResult(false, $"File '{path}' does not exist.")); } if (Exists && Folder && !Directory.Exists(path)) { return(new CommandResult(false, $"Folder '{path}' does not exist.")); } if (Readable && !FileSupport.IsFileReadable(path)) { return(new CommandResult(false, $"File '{path}' is not readable.")); } if (Writable && !FileSupport.IsFileWritable(path)) { return(new CommandResult(false, $"File '{path}' is not writable.")); } arg.ResolvedValue = path; } return(result); }
private CommandLineProto Find(CommandLineProto[] protos, int pos, CommandLine args) { CommandArg name = pos < args.Count ? args[pos] : null; if (name == null || name.IsSwitch) { foreach (var proto in protos) { if (proto.HasRequired(args)) { return(proto); } } return(null); } else { List <CommandLineProto> matches = new List <CommandLineProto>(); foreach (var proto in protos) { var protoArg = proto.GetPositional(pos) as CommandArgProto; if (protoArg != null && (!protoArg.IsCommand || string.Compare(protoArg.Value, name.Value, true) == 0)) { matches.Add(proto); } } if (matches.Count == 0) { return(null); } else if (matches.Count == 1) { return(matches[0]); } else { return(Find(matches.ToArray(), pos + 1, args)); } } }
public CommandLineProto Find(CommandLine args) { if (args != null) { CommandArg name = args.GetPositional(0); if (name != null) { CommandLineProto[] protos = Find(name.Value); if (protos.Length == 0) { return(null); } else if (protos.Length == 1) { return(protos[0]); } else { return(Find(protos, 1, args)); } } } return(null); }
public CommandResult Validate(CommandArg arg) { return(_validator.Validate(arg)); }