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.