]> git.tdb.fi Git - libs/net.git/commitdiff
Update getopt usage with positional arguments
authorMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 16:28:52 +0000 (18:28 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 16:41:46 +0000 (18:41 +0200)
examples/httpget.cpp
examples/netcat.cpp

index a4314cef6e3ec12ee83114d36f3e6bd0a823ae71..18ef79933791849bd79dddaf307bf76285fa0e6a 100644 (file)
@@ -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<string> &args = getopt.get_args();
-       if(args.empty())
-               throw usage_error("No URL");
-
-       url = args.front();
 }
 
 int HttpGet::main()
index 9bbad87b8d0fa944a636cf52e28ba37f67d7e8ee..cd549f4a045e9486f92d9c01da5fe04edda9c16a 100644 (file)
@@ -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<string> &args = getopt.get_args();
-       if(args.empty())
-               throw usage_error("host argument missing");
-
-       RefPtr<Net::SockAddr> addr = Net::resolve(args.front(), (ipv6 ? Net::INET6 : Net::INET));
+       RefPtr<Net::SockAddr> addr = Net::resolve(host_name, (ipv6 ? Net::INET6 : Net::INET));
        if(!listen)
        {
                sock = new Net::StreamSocket(addr->get_family());