News:

Use the "Forum Search"
It may help you to find anything in the forum ;).

Patch to improve structural aspects of nettool (work in progress)

Started by Ashley, May 22, 2011, 11:19:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ashley

This is one of the patches I am working on for the nettool utility. This one reworks some of the structure of the tool to hopefully make it easier to extend with additional methods. The command line arguments are changed to be split into options (which use classic unix-style flags, e.g. -s for server) and commands. E.g. to connect to server "myserver.com:13354" with password "mypassword" and suppress the copyright message to run the "say" command with the argument "hello world" you'd run:

nettool -q -s myserver.com:13354 -p mypassword say "hello world"

The options parser is fairly flexible, so you can put them in different orders, or combine them, e.g.:

nettool -qs myserver.com:13354 -pmypassword say "hello world"

Is still valid. It can check for invalid options and will print the usage statement if problems are found. The password can either be provided on the command line by the -p switch, or read in from a file or stdin using the -P switch (in which case the "Password: " prompt isn't displayed), e.g. you could do this:

echo mypassword > passwd.txt
nettool -q -P - -s myserver.com:13354 say "hello world" < passwd.txt

Or this:

echo mypassword > passwd.txt
nettool -q -P passwd.txt -s myserver.com:13354 say "hello world"

Each command is split into its own function, and these are looked up by command ID in an array of structs. I think this is a lot clearer and makes it really easy to add additional commands. (This still needs some improvement)

There are still a couple of TODOs in there, like not echoing the password and not writing the Password: prompt if stdin isn't a terminal but I wanted to get some feedback on this since it's almost my first foray into C/C++ coding.

Edit: Final patch in post below (version 5), tested on Windows and seems to work fine. This is set so that if a password isn't provided it prompts for one and doesn't echo it to the terminal. I've updated the description above.
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

wlindley

Grammar nitpick to whoever wrote the line in history.txt:

The word is "administer" not "administrate."  A poetic reminder:

----

If you use the word, "Administrate"
Your grammar I won't admirate
If your computer is configurated
To say it is a word, you'll have to reinterpretate
What it says, 'cause it's not, I have been reliably informated.

Dwachs

@Timothy: looks good ! One thing, the naming convention for classes in simutrans is something like "fetchopt_t"  :police:

@Wlindley:  ;D Thanks for that piece of lyrics!
Parsley, sage, rosemary, and maggikraut.

Ashley

Quote from: Dwachs on May 24, 2011, 03:40:05 PM
@Timothy: looks good ! One thing, the naming convention for classes in simutrans is something like "fetchopt_t"  :police:

Naming issue fixed, along with the rest of the TODO items. New patch attached, I have tried to document the fetchopt_t class in the header file extensively. I might write a similar options parser which can cope with long single-dash options like those used by the main game too...

This patch should be final unless there are any major issues you can see with it :)
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Dwachs

Parsley, sage, rosemary, and maggikraut.