From: Mikko Rasa Date: Fri, 9 Dec 2022 16:28:52 +0000 (+0200) Subject: Update getopt usage with positional arguments X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=commitdiff_plain;h=c0b73c437d75a6fbf111969ea67c1d24586dbde7 Update getopt usage with positional arguments --- diff --git a/examples/httpget.cpp b/examples/httpget.cpp index a4314ce..18ef799 100644 --- a/examples/httpget.cpp +++ b/examples/httpget.cpp @@ -28,13 +28,8 @@ HttpGet::HttpGet(int argc, char **argv): { GetOpt getopt; getopt.add_option('v', "verbose", verbose, GetOpt::NO_ARG); + getopt.add_argument("url", url, GetOpt::REQUIRED_ARG); getopt(argc, argv); - - const vector &args = getopt.get_args(); - if(args.empty()) - throw usage_error("No URL"); - - url = args.front(); } int HttpGet::main() diff --git a/examples/netcat.cpp b/examples/netcat.cpp index 9bbad87..cd549f4 100644 --- a/examples/netcat.cpp +++ b/examples/netcat.cpp @@ -34,16 +34,15 @@ NetCat::NetCat(int argc, char **argv): server_sock(0), sock(0) { + string host_name; + GetOpt getopt; getopt.add_option('6', "ipv6", ipv6, GetOpt::NO_ARG); getopt.add_option('l', "listen", listen, GetOpt::NO_ARG); + getopt.add_argument("host", host_name, GetOpt::REQUIRED_ARG); getopt(argc, argv); - const vector &args = getopt.get_args(); - if(args.empty()) - throw usage_error("host argument missing"); - - RefPtr addr = Net::resolve(args.front(), (ipv6 ? Net::INET6 : Net::INET)); + RefPtr addr = Net::resolve(host_name, (ipv6 ? Net::INET6 : Net::INET)); if(!listen) { sock = new Net::StreamSocket(addr->get_family());