Send several parameters for args [JAVA]

0

I am learning java and I would like to solve this problem

I have a program that executes 2 subprocesses (Son1 and Son2) which I send data through a file (file1) and with a redirect I return the result to another file (file2) when executing the program by bash I want to add a "-p" to indicate the processes to execute and then "-a" to indicate the resulting files, something like this:

  

java Father -p Son1 Son2 -a file1.txt file2.txt

I have no problem with the code, since it works correctly but I would like to use that "-p" to indicate the processes and "-a" to indicate the routes of the files.

Probably something very simple but I'm not very familiar with args.

I had thought about a loop reading all the args with args.length and looking for that argument is "-p" or "-a" with args [i] .equals ("- p") but there I stay stuck

    
asked by dePuertoReal 26.10.2018 в 00:54
source

1 answer

0

A while ago I made an application in the console that received some input parameters. The application is in C # but I think it can be translated into Java easily.

What I do is get all the strings that come in the array args [] and iterate looking for the argument lines "-X" followed by its value args[( argumentoEncontrado)+1]

The snippet is something like this:

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            string ayudastr = "Parámetros de entrada\n"+
             "Param     Descripción\n"+
             " -h       Ayuda. Este texto\n"+
             " -r       Solo mostrar (No eliminar)\n" +
             " -v       Verbose, salida a consola del detalle del proceso\n"+
             " -q       No pedir confirmaciones.\n" +
             " -e       Pedir confirmación para eliminar.\n" +
             " -u '' *  Usuario del cliente\n"+
             " -p '' *  Contraseña del cliente\n"+
             " -S '' *  Host IMAP\n"+
             " -P ''    Puerto del host\n"+
             " -s       Usar SSL\n" +
             " -T       Usar TLS\n" +
             " -d '' *  Días atrás a eliminar\n"+
             " -f '' *  Carpeta donde buscar mensajes. Case sensitive."; 
            foreach (string s in args)
                Console.Write(s + " ");
            Console.WriteLine("");
            int iUsr=-1, iPass=-1, iServer=-1, iDias=-1, iFolder=-1, iPuerto=-1;
            for (int i = 0; i < args.Count(); i++)
                switch (args[i])
                {
                    case "-u":
                        iUsr = i + 1;
                        break;
                    case "-p":
                        iPass = i + 1;
                        break;
                    case "-P":
                        iPuerto = i + 1;
                        break;
                    case "-S":
                        iServer = i + 1;
                        break;
                    case "-d":
                        iDias = i + 1;
                        break;
                    case "-f":
                        iFolder = i + 1;
                        break;
                }
            if (args.Contains("-h"))
            {
                Console.Write(ayudastr);
                Console.ReadLine();
                return;
            }
            string user = "";
            string pass = "";
            string server = "";
            double dias = 0;
            string fechastr = "";
            string folderemail = "";
            try
            {
                user = args[iUsr];
                pass = args[iPass];
                server = args[iServer];
                dias = -1 * Convert.ToDouble(args[iDias]);
                fechastr = DateTime.Now.AddDays(dias).ToString("dd-MMM-yyyy");
                folderemail = args[iFolder];
            }
            catch (Exception ex)
            {
                Console.WriteLine("Faltan parámetros necesarios");
                Console.ReadLine();
                return;
            }
            string puerto = "993";//IMAP default
            bool verbose = false;
            bool useSSL = false;
            bool useTLS = false;
            bool validarCert = false;
            bool pedirConfirmacionEliminar = false;
            bool pedirConfirmacion = false;
            bool soloMostrar = false;
            if (iPuerto != -1)
                puerto = args[iPuerto];
            if (args.Contains("-s"))
                useSSL = true;
            if (args.Contains("-T"))
                useTLS = true;
            if (args.Contains("-r"))
                soloMostrar = true;
            if (args.Contains("-e"))
                pedirConfirmacionEliminar = true;
            if (args.Contains("-q"))
                pedirConfirmacion= true;

Surely there are easier ways to do it but this is what worked for me at the time.

    
answered by 26.10.2018 в 01:31