Merl's Blog

Flags

When you are learning to get better at something, once you learn a new move it seems like that becomes your hammer. And once you have your new hammer, everything looks like a nail.

For me this new hammer has been flags. Flags to make things happen but also flags to help test.

As of this moment I am unsure if this is good practice or I need to refactor more, but I I do have to refactor more this was a nice bandaid to keep me going.

Recently I wanted to add functionality to my argos into my main.

Before I just had “-p” which controlled the which port the server connected to, and “-r” too change the root directory from the server source.

Now I am adding in “-x” and “-h”. “-h” is for help and all it does is return what everything does.

That was straightforward and simple. Now was “-x” which prints out the start up configuration without starting the server.

Currently this is my main:

```java public static void main(String[] args) throws IOException { String outputMessage = parseArgs(args);

if (outputMessage != null) { System.out.println(outputMessage); } else { MyHTTPServer main = new MyHTTPServer(port, rootDirectory); main.start(); } }


Where `parseArgs` returns null if -x or -h are called.

When “-h” is called then the help message is given, and it is printed. When “-x” is given the configuration is printed out as well.

I had a minor problem though. My parse method has a for loop and it goes through everything one by one. When I had my original test and code it immediately send the config message and that was it.

```java
} else if ("-x".equals(args[i])) {
   return getConfigMessage();
}

This was great when the “-x” was put last. However if the arguments started with “-x” and then had this “-r”, “testroot” it would not place the new root directory.

I did not want to create another loop since I had 1 already, so I decided to do the next best thing.

else if ("-x".equals(args[i])) {
       xFlag = true;
   }
}
if (xFlag) {
   outputMessage = getConfigMessage();
}


return outputMessage;

I placed my flag and I conquered it. We will see if that flag will last the test of time. But for now, there it remains.

Best,

Merl