From c0b73c437d75a6fbf111969ea67c1d24586dbde7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 9 Dec 2022 18:28:52 +0200 Subject: [PATCH] Update getopt usage with positional arguments --- examples/httpget.cpp | 7 +------ examples/netcat.cpp | 9 ++++----- 2 files changed, 5 insertions(+), 11 deletions(-) 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()); -- 2.45.2