Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/cotuba/CommandLineOptions.java
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}."));
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peguei um bug aqui

Suggested change
VERBOSE(new Option("o", "output", true,
"Arquivo de saída do ebook. Default: book.{formato}."));
VERBOSE(new Option("v", "verbose", false, "Habilita modo verboso."));

Option opcaoModoVerboso = new Option("v", "verbose", false,
"Habilita modo verboso.");

Copy link
Author

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.

Copy link
Contributor

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


private Option option;

CommandLineOptions(Option option) {
this.option = option;
}

public Option getOption() {
return this.option;
}
}
97 changes: 97 additions & 0 deletions src/main/java/cotuba/LeitorOpcoesCLI.java
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 getOptions que cria o Options e adiciona cada uma. Toda enum tem um método values retorna um Array com os valores da enum. Aí, você pode fazer um for each, e ir pegando o option e adicionando.

Aí, teria um getOptions na própria enum e não precisaria do getOption.

O que acha?


public Path getDiretorioDosMD() {
return diretorioDosMD;
}

public String getFormato() {
return formato;
}

public Path getArquivoDeSaida() {
return arquivoDeSaida;
}

public boolean isModoVerboso() {
return modoVerboso;
}
}
Loading