package fi.helsinki.cs.tmc.cli.command;

import fi.helsinki.cs.tmc.cli.Application;

import java.io.IOException;
import java.util.Scanner;

public class LoginCommand implements Command {

    // todo: use our own terminal IO when available
    private final Scanner scanner;

    private Application app;

    public LoginCommand(Application app) {
        this.app = app;
        this.scanner = new Scanner(System.in);
    }

    @Override
    public String getDescription() {
        return "Login to TMC server.";
    }

    @Override
    public String getName() {
        return "login";
    }

    @Override
    public void run(String[] args) {
        String username = null;
        String password = null;

        try {
            username = readLine("username: ");
            password = readPassword("password: ");
        } catch (Exception e) {
            // todo: Logger
        }

        //System.out.println(username + ":" + password);
    }

    // todo: use our own terminal IO when available
    private String readLine(String prompt) throws IOException {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    // todo: use our own terminal IO when available
    private String readPassword(String prompt) throws IOException {
        // Read the password in cleartext if no console is present (might happen
        // in some IDEs?)
        if (System.console() != null) {
            char[] pwd = System.console().readPassword(prompt);
            return new String(pwd);
        }
        return this.readLine(prompt);
    }
}

@@ -78,16 +78,186 @@
 ation;%0A%0A
+import org.apache.commons.cli.CommandLine;%0Aimport org.apache.commons.cli.GnuParser;%0Aimport org.apache.commons.cli.Options;%0Aimport org.apache.commons.cli.ParseException;%0A%0A
 import j
@@ -439,16 +439,88 @@
 anner;%0A%0A
+    private final GnuParser parser;%0A    private final Options options;%0A%0A
     priv
@@ -655,16 +655,230 @@
 em.in);%0A
+        this.parser = new GnuParser();%0A        this.options = new Options();%0A        options.addOption(%22u%22, %22user%22, true, %22TMC username%22);%0A        options.addOption(%22p%22, %22password%22, true, %22Password for the user%22);%0A
     %7D%0A%0A 
@@ -1194,43 +1194,302 @@
     
-username = readLine(%22username: %22);%0A
+CommandLine line = this.parser.parse(options, args);%0A%0A            username = line.getOptionValue(%22u%22);%0A            if (username == null) %7B%0A                username = readLine(%22username: %22);%0A            %7D%0A%0A            password = line.getOptionValue(%22p%22);%0A            if (password == null) %7B%0A    
     
@@ -1531,24 +1531,38 @@
 ssword: %22);%0A
+            %7D%0A
         %7D ca
@@ -1566,16 +1566,35 @@
  catch (
+ParseException %7C IO
 Exceptio
@@ -1649,16 +1649,44 @@
       //
+ todo: do something%0A        
 System.o
@@ -1708,17 +1708,19 @@
 name + %22
-:
+ : 
 %22 + pass

Add support for command line arguments