public static Opts Run(string[] args) { Opts opts = new Opts(); bool showHelp = false; var CommandLineOpts = new BeeOptsBuilder() .Add('b', "base", OPTTYPE.VALUE, "basedir", (v) => opts.baseDir = v) .Add('p', "path", OPTTYPE.BOOL, "create path", (v) => opts.createPath = true) .Add('h', "help", OPTTYPE.BOOL, "show this help", (v) => showHelp = true) .GetOpts(); IList <string> pargs = BeeOpts.Parse(args, CommandLineOpts, (optname) => Console.Error.WriteLine($"unknow option: [{optname}]")); if (showHelp) { opts = null; PrintUsage(CommandLineOpts); } else if (pargs.Count == 1) { opts.filename = pargs[0]; } else { opts = null; PrintUsage(CommandLineOpts); } return(opts); }
public static bool ParseOpts(string[] args, out Opts opts) { opts = null; bool showhelp = false; Opts tmpOpts = new Opts() { }; var cmdOpts = new BeeOptsBuilder() .Add('e', "err", OPTTYPE.VALUE, "filename for error messages", o => tmpOpts.errorFilename = o) .Add('v', "verbose", OPTTYPE.BOOL, "show some debug output", o => tmpOpts.verbose = true) .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true) .GetOpts(); tmpOpts.Filenames = BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]")); if (showhelp) { Console.WriteLine( "\nusage: PXELogParser [OPTIONS] [Filenames...]" + "\n\nOptions:"); BeeOpts.PrintOptions(cmdOpts); return(false); } opts = tmpOpts; return(true); }
public static Opts Parse(string[] args) { Opts opts = new Opts(); bool showHelp = false; IList <BeeOpts> CommandLineOpts = new BeeOptsBuilder() .Add('d', "depth", OPTTYPE.VALUE, "max depth to go down", v => opts.Depth = Convert.ToInt32(v)) .Add('j', "follow", OPTTYPE.BOOL, "follow junctions", v => opts.FollowJunctions = (v != null)) .Add('t', "threads", OPTTYPE.VALUE, "max enumeration threads parallel", v => opts.MaxThreads = Convert.ToInt32(v)) .Add('s', "same", OPTTYPE.BOOL, "report equal files (same.txt)", v => opts.reportSameFile = true) .Add(null, "sorts", OPTTYPE.BOOL, "force sorting of entries on source side", v => opts.forceSortSource = true) .Add(null, "sortt", OPTTYPE.BOOL, "force sorting of entries on target side", v => opts.forceSortTarget = true) .Add('h', "help", OPTTYPE.BOOL, "show this help", v => showHelp = true) .GetOpts(); IList <string> dirs = BeeOpts.Parse( args, CommandLineOpts, OnUnknown: (optname) => Console.Error.WriteLine($"unknow option: [{optname}]")); if (showHelp) { opts = null; PrintUsage(CommandLineOpts); } else { if (dirs.Count != 2) { Console.Error.WriteLine("no two dir's given"); opts = null; showHelp = true; } else { opts.sourceDir = dirs[0]; opts.targetDir = dirs[1]; } if (opts != null && opts.forceSortSource) { Console.Error.WriteLine("will sort items in source dir"); } if (opts != null && opts.forceSortTarget) { Console.Error.WriteLine("will sort items in target dir"); } if (showHelp) { Spi.BeeOpts.PrintOptions(CommandLineOpts); opts = null; } } return(opts); }
public void NoOptions() { var opts = new BeeOptsBuilder() .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "a", "b" }, opts, (optname) => throw new Exception($"unknow {optname}")); Assert.AreEqual(2, args.Count); }
public void OneOptionWithoutSpace() { string a = null; var opts = new BeeOptsBuilder() .Add('a', "add", OPTTYPE.VALUE, "add more args", (v) => a = v) .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "-ab" }, opts, (optname) => throw new Exception($"unknow {optname}")); Assert.AreEqual("b", a); }
public static bool ParseOpts(string[] args, out Opts opts) { opts = null; bool showhelp = false; Opts tmpOpts = new Opts() { }; var cmdOpts = new BeeOptsBuilder() .Add('m', "mac", OPTTYPE.VALUE, "mac address for ", o => tmpOpts.MAC = o) .Add(null, "macfile", OPTTYPE.VALUE, "input file with MAC addresses", o => tmpOpts.FilenameMacAddresses = o) .Add('s', "broadcast", OPTTYPE.VALUE, "IP address to send to", o => tmpOpts.broadcastIP = o) .Add(null, "broadcastfile", OPTTYPE.VALUE, "input file with subnet broadcast IPs", o => tmpOpts.FilenameBroadcastIPs = o) .Add('c', "cidr", OPTTYPE.VALUE, "CIDR of subnet", o => tmpOpts.cidr = o) .Add(null, "cidrfile", OPTTYPE.VALUE, "input file with CIDRs", o => tmpOpts.FilenameCIDRs = o) .Add('v', "verbose", OPTTYPE.BOOL, "show some output", o => tmpOpts.verbose = true) .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true) .GetOpts(); var parsedArgs = BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]")); if (String.IsNullOrEmpty(tmpOpts.MAC) && String.IsNullOrEmpty(tmpOpts.FilenameMacAddresses)) { Console.Error.WriteLine("no MAC of file with MACs given"); showhelp = true; } if (String.IsNullOrEmpty(tmpOpts.broadcastIP) && String.IsNullOrEmpty(tmpOpts.FilenameBroadcastIPs) && String.IsNullOrEmpty(tmpOpts.cidr) && String.IsNullOrEmpty(tmpOpts.FilenameCIDRs)) { tmpOpts.broadcastIP = "255.255.255.255"; if (tmpOpts.verbose) { Console.WriteLine($"setting broadcast IP to {tmpOpts.broadcastIP}"); } } if (showhelp) { Console.WriteLine( "\nusage: wol.exe [OPTIONS]" + "\n\nOptions:"); BeeOpts.PrintOptions(cmdOpts); return(false); } opts = tmpOpts; return(true); }
public void shortBoolOptionWithmoreThanOneChar() { bool x = false; List <string> unknownOptions = new List <string>(); var opts = new BeeOptsBuilder() .Add('x', "req", OPTTYPE.BOOL, "desc", (v) => x = true) .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "-xbumsti" }, opts, OnUnknown: (optname) => unknownOptions.Add(optname)); Assert.IsTrue(x); CollectionAssert.AreEqual(new string[] { "b", "u", "m", "s", "t", "i" }.ToList(), unknownOptions); }
public void TwoValueOptions_one_not_given_other_order() { string r = null; string o = null; var opts = new BeeOptsBuilder() .Add('r', "req", OPTTYPE.VALUE, "required", (v) => r = v) .Add('o', "opt", OPTTYPE.VALUE, "optional", (v) => o = v) .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "-o", "-r", "r" }, opts, (optname) => throw new Exception($"unknow {optname}")); Assert.AreEqual("r", r); Assert.IsNull(o); }
public void TwoOptions_long_short_with_value_2() { string r = null; string b = null; var opts = new BeeOptsBuilder() .Add('r', "req", OPTTYPE.VALUE, "desc", (v) => r = v) .Add('b', "boo", OPTTYPE.VALUE, "desc", (v) => b = v) .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "--boo", "huu", "-r", "R" }, opts, (optname) => throw new Exception($"unknow option [{optname}]")); Assert.AreEqual("R", r); Assert.AreEqual("huu", b); }
public void TwoOptions_bool_value_long_notation() { string r = null; bool b = false; var opts = new BeeOptsBuilder() .Add('r', "req", OPTTYPE.VALUE, "desc", (v) => r = v) .Add('b', "boo", OPTTYPE.BOOL, "desc", (v) => b = true) .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "--boo", "-rR" }, opts, (optname) => throw new Exception($"unknow option [{optname}]")); Assert.AreEqual("R", r); Assert.IsTrue(b); }
public static bool ParseOpts(string[] args, out Opts opts) { opts = null; bool showhelp = false; Opts tmpOpts = new Opts() { }; var cmdOpts = new BeeOptsBuilder() .Add('s', "SiteURL", OPTTYPE.VALUE, "Sharepoint site URL", o => tmpOpts.SiteURL = o) .Add('l', "list", OPTTYPE.VALUE, "name of the list to process", o => tmpOpts.listname = o) .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true) .GetOpts(); tmpOpts.command = BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]")); if (String.IsNullOrEmpty(tmpOpts.SiteURL)) { showhelp = true; Console.Error.WriteLine("SiteURL is missing"); } if (String.IsNullOrEmpty(tmpOpts.listname)) { showhelp = true; Console.Error.WriteLine("name of the list is missing"); } if (showhelp) { Console.WriteLine( "\nusage: SPUtil.exe [OPTIONS] {command}" + "\n\nOptions:"); BeeOpts.PrintOptions(cmdOpts); Console.WriteLine( "\nCommand:" + "\n l ........ list" + "\n d {id} ... delete item with {id}" + "\n da ....... delete all items"); return(false); } opts = tmpOpts; return(true); }
public void unknowOption() { string r = "n/a"; string b = null; var opts = new BeeOptsBuilder() .Add('r', "req", OPTTYPE.VALUE, "desc", (v) => r = v) .Add('b', "boo", OPTTYPE.VALUE, "desc", (v) => b = v) .GetOpts(); bool unknownOption = false; IList <string> args = BeeOpts.Parse( new string[] { "-j" }, opts, OnUnknown: (optname) => unknownOption = true); Assert.IsTrue(unknownOption); }
public static bool ParseOpts(string[] args, out Opts opts) { opts = null; bool showhelp = false; string IPList = null; Opts tmpOpts = new Opts() { Port = 4444 }; var cmdOpts = new BeeOptsBuilder() .Add('p', "port", OPTTYPE.VALUE, "udp port to listen", o => tmpOpts.Port = Convert.ToUInt16(o)) .Add('i', "ip", OPTTYPE.VALUE, "ips for packet to inject", o => IPList = o) .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true) .GetOpts(); var IPsString = Spi.BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]")); if (showhelp) { Spi.BeeOpts.PrintOptions(cmdOpts); Console.WriteLine( "\nSample:" + "UdpRacer.exe -i 10.2.2.2,10.1.1.1" ); return(false); } if (!String.IsNullOrEmpty(IPList)) { if (!TryParseIPs( IpsAsString: IPList.Split(',').Select(i => i.Trim()), IPs: out tmpOpts.IPs)) { return(false); } } opts = tmpOpts; return(true); }
public void shortBoolFollowedByValueInNextArg() { List <string> unknownOptions = new List <string>(); bool b = false; string val = String.Empty; var opts = new BeeOptsBuilder() .Add('b', "req", OPTTYPE.BOOL, "descb", (v) => b = true) .Add('v', "vvv", OPTTYPE.VALUE, "descv", (v) => val = v) .GetOpts(); IList <string> args = BeeOpts.Parse( new string[] { "-bv", "bumsti" }, opts, OnUnknown: (optname) => unknownOptions.Add(optname)); CollectionAssert.AreEqual(new string[] { }, unknownOptions); Assert.IsTrue(b); Assert.AreEqual("bumsti", val); }
public static bool ParseOpts(string[] args, out Opts opts, out List <string> commandlineTemplate) { opts = null; bool showhelp = false; string defaultOutFilename = @".\forp.out.txt"; Opts tmpOpts = new Opts() { }; var cmdOpts = new BeeOptsBuilder() .Add('f', "file", OPTTYPE.VALUE, "input file", o => tmpOpts.inputfilename = o) .Add('c', "cmd", OPTTYPE.BOOL, "execute with [%ComSpec% /C]", o => tmpOpts.runCmdExe = true) .Add('o', "out", OPTTYPE.VALUE, $"filename for stdout. default: {defaultOutFilename}", o => tmpOpts.filenameStdout = o) .Add('e', "err", OPTTYPE.VALUE, $"filename for stderr. default: {defaultOutFilename}", o => tmpOpts.filenameStderr = o) .Add('1', "first", OPTTYPE.BOOL, "run only for first line in inputfile", o => tmpOpts.firstOnly = true) .Add('n', "dryrun", OPTTYPE.BOOL, "dry run", o => tmpOpts.dryrun = true) .Add('s', "skipempty", OPTTYPE.BOOL, "do not write empty lines to output. String.IsNullOrWhiteSpace()", o => tmpOpts.skipEmptyLines = true) .Add('q', "quiet", OPTTYPE.BOOL, "don't print anything", o => tmpOpts.quiet = true) .Add('x', "parallel", OPTTYPE.VALUE, $"run max parallel processes (default: {tmpOpts.maxParallel})", o => tmpOpts.maxParallel = Convert.ToInt32(o)) .Add('d', "debug", OPTTYPE.BOOL, "debug output", o => tmpOpts.debug = true) .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true) .Add(null, "stdout", OPTTYPE.BOOL, "write stdout/stderr of processes to stdout", o => tmpOpts.allToStdout = true) .Add(null, "noprefix", OPTTYPE.BOOL, "do not prefix every output line with %1", o => tmpOpts.printPrefix = false) .Add(null, "noerr", OPTTYPE.BOOL, "do not capture stderr", o => tmpOpts.writeStderr = false) .GetOpts(); commandlineTemplate = Spi.BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]")); if (showhelp) { Console.WriteLine( "\nusage: forp.exe [OPTIONS] -- [commandline to execute. use %1, %2, ... as tokens from your input]" + "\n\nOptions:"); Spi.BeeOpts.PrintOptions(cmdOpts); Console.WriteLine( "\n + if no inputfile is given. read input from stdin" + "\n + each line from the input is parsed with CommandLineToArgv() to produce %1, %2, ..." + "\n\n generated files:" + "\n + forp.out.txt ........ stdout and stderr from all executed processes. each line is prepended with %1" + "\n + forp.ExitCode.txt ... exitcode of each executed process. {rc}TAB{commandline}" + "\n\n defaults:" + "\n 1, if just one program (exe/...) is passed as commandline, all the tokens form your input will be appended to it" + "\n 2, if no commandline is given the lines of your input are treated as a commandline to execute" ); return(false); } if (!String.IsNullOrEmpty(tmpOpts.inputfilename)) { if (!File.Exists(tmpOpts.inputfilename)) { Console.Error.WriteLine($"cannot find inputfile given [{tmpOpts.inputfilename}]"); return(false); } } if (tmpOpts.allToStdout) { if (!(String.IsNullOrEmpty(tmpOpts.filenameStdout) && String.IsNullOrEmpty(tmpOpts.filenameStderr))) { Console.Error.WriteLine($"you have specified to write everything to stdout, but also give a filename for out/err."); return(false); } } else { if (tmpOpts.writeStderr) { } else { if (!String.IsNullOrEmpty(tmpOpts.filenameStderr)) { Console.Error.WriteLine($"you have specified NOT TO WRITE stderr but a filename of [{tmpOpts.filenameStderr}] was given."); return(false); } } if (String.IsNullOrEmpty(tmpOpts.filenameStdout)) { tmpOpts.filenameStdout = defaultOutFilename; } } opts = tmpOpts; return(true); }