-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Playground #1
base: master
Are you sure you want to change the base?
Playground #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cotuba; | ||
|
||
import org.apache.commons.cli.Option; | ||
|
||
public enum CommandLineOptions { | ||
DIR(new Option("d", "dir", true, | ||
"Diretório que contem os arquivos md. Default: diretório atual.")), | ||
FORMAT(new Option("f", "format", true, | ||
"Formato de saída do ebook. Pode ser: pdf ou epub. Default: pdf")), | ||
OUTPUT(new Option("o", "output", true, | ||
"Arquivo de saída do ebook. Default: book.{formato}.")), | ||
VERBOSE(new Option("o", "output", true, | ||
"Arquivo de saída do ebook. Default: book.{formato}.")); | ||
|
||
private Option option; | ||
|
||
CommandLineOptions(Option option) { | ||
this.option = option; | ||
} | ||
|
||
public Option getOption() { | ||
return this.option; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cotuba; | ||
|
||
import org.apache.commons.cli.*; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class LeitorOpcoesCLI { | ||
|
||
private Path diretorioDosMD; | ||
private String formato; | ||
private Path arquivoDeSaida; | ||
boolean modoVerboso = false; | ||
|
||
LeitorOpcoesCLI(String[] args) { | ||
Options options = initializeOptions(); | ||
|
||
CommandLineParser cmdParser = new DefaultParser(); | ||
HelpFormatter ajuda = new HelpFormatter(); | ||
CommandLine cmd; | ||
|
||
try { | ||
cmd = cmdParser.parse(options, args); | ||
} catch (ParseException e) { | ||
System.err.println(e.getMessage()); | ||
ajuda.printHelp("cotuba", options); | ||
System.exit(1); | ||
return; | ||
} | ||
|
||
String nomeDoDiretorioDosMD = cmd.getOptionValue("dir"); | ||
|
||
if (nomeDoDiretorioDosMD != null) { | ||
diretorioDosMD = Paths.get(nomeDoDiretorioDosMD); | ||
if (!Files.isDirectory(diretorioDosMD)) { | ||
throw new RuntimeException(nomeDoDiretorioDosMD + " não é um diretório."); | ||
} | ||
} else { | ||
Path diretorioAtual = Paths.get(""); | ||
diretorioDosMD = diretorioAtual; | ||
} | ||
|
||
String nomeDoFormatoDoEbook = cmd.getOptionValue("format"); | ||
|
||
if (nomeDoFormatoDoEbook != null) { | ||
formato = nomeDoFormatoDoEbook.toLowerCase(); | ||
} else { | ||
formato = "pdf"; | ||
} | ||
|
||
String nomeDoArquivoDeSaidaDoEbook = cmd.getOptionValue("output"); | ||
if (nomeDoArquivoDeSaidaDoEbook != null) { | ||
arquivoDeSaida = Paths.get(nomeDoArquivoDeSaidaDoEbook); | ||
if (Files.exists(arquivoDeSaida) && Files.isDirectory(arquivoDeSaida)) { | ||
throw new RuntimeException(nomeDoArquivoDeSaidaDoEbook + " é um diretório."); | ||
} | ||
} else { | ||
arquivoDeSaida = Paths.get("book." + formato.toLowerCase()); | ||
} | ||
|
||
modoVerboso = cmd.hasOption("verbose"); | ||
} | ||
|
||
private Options initializeOptions() { | ||
Options options = new Options(); | ||
|
||
Option opcaoDeDiretorioDosMD = CommandLineOptions.DIR.getOption(); | ||
options.addOption(opcaoDeDiretorioDosMD); | ||
|
||
Option opcaoDeFormatoDoEbook = CommandLineOptions.FORMAT.getOption(); | ||
options.addOption(opcaoDeFormatoDoEbook); | ||
|
||
Option opcaoDeArquivoDeSaida = CommandLineOptions.OUTPUT.getOption(); | ||
options.addOption(opcaoDeArquivoDeSaida); | ||
|
||
Option opcaoModoVerboso = CommandLineOptions.VERBOSE.getOption(); | ||
options.addOption(opcaoModoVerboso); | ||
return options; | ||
} | ||
Comment on lines
+65
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui, talvez a responsabilidade de criar as opções possa ser da própria enum. Na enum, você pode ter um método Aí, teria um O que acha? |
||
|
||
public Path getDiretorioDosMD() { | ||
return diretorioDosMD; | ||
} | ||
|
||
public String getFormato() { | ||
return formato; | ||
} | ||
|
||
public Path getArquivoDeSaida() { | ||
return arquivoDeSaida; | ||
} | ||
|
||
public boolean isModoVerboso() { | ||
return modoVerboso; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Peguei um bug aqui
Option opcaoModoVerboso = new Option("v", "verbose", false,
"Habilita modo verboso.");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sim, um dos problemas de usar CTRL+ C e CTRL+ V sem prestar atenção, vou corrigir.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esse problema é um clássico! Hehe