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

Refatorando as opções de command line #2

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ target
.project
.settings
.build/
.idea/
cotuba-cli.iml

Comment on lines +8 to +10
Copy link
Contributor

Choose a reason for hiding this comment

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

Boa!


39 changes: 6 additions & 33 deletions src/main/java/cotuba/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.stream.Stream;

import cotuba.cli.OptionCLIReader;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand Down Expand Up @@ -40,36 +41,8 @@
public class Main {

public static void main(String[] args) {
Options options = new Options();

Option opcaoDeDiretorioDosMD = new Option("d", "dir", true,
"Diretório que contem os arquivos md. Default: diretório atual.");
options.addOption(opcaoDeDiretorioDosMD);

Option opcaoDeFormatoDoEbook = new Option("f", "format", true,
"Formato de saída do ebook. Pode ser: pdf ou epub. Default: pdf");
options.addOption(opcaoDeFormatoDoEbook);

Option opcaoDeArquivoDeSaida = new Option("o", "output", true,
"Arquivo de saída do ebook. Default: book.{formato}.");
options.addOption(opcaoDeArquivoDeSaida);

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

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;
}
OptionCLIReader optionCLIReader = new OptionCLIReader(args);

Path diretorioDosMD;
String formato;
Expand All @@ -78,7 +51,7 @@ public static void main(String[] args) {

try {

String nomeDoDiretorioDosMD = cmd.getOptionValue("dir");
String nomeDoDiretorioDosMD = optionCLIReader.getNomeDoDiretorioDosMD();

if (nomeDoDiretorioDosMD != null) {
diretorioDosMD = Paths.get(nomeDoDiretorioDosMD);
Expand All @@ -90,15 +63,15 @@ public static void main(String[] args) {
diretorioDosMD = diretorioAtual;
}

String nomeDoFormatoDoEbook = cmd.getOptionValue("format");
String nomeDoFormatoDoEbook = optionCLIReader.getNomeDoFormatoDoEbook();

if (nomeDoFormatoDoEbook != null) {
formato = nomeDoFormatoDoEbook.toLowerCase();
} else {
formato = "pdf";
}

String nomeDoArquivoDeSaidaDoEbook = cmd.getOptionValue("output");
String nomeDoArquivoDeSaidaDoEbook = optionCLIReader.getNomeDoArquivoDeSaidaDoEbook();
if (nomeDoArquivoDeSaidaDoEbook != null) {
arquivoDeSaida = Paths.get(nomeDoArquivoDeSaidaDoEbook);
if (Files.exists(arquivoDeSaida) && Files.isDirectory(arquivoDeSaida)) {
Expand All @@ -108,7 +81,7 @@ public static void main(String[] args) {
arquivoDeSaida = Paths.get("book." + formato.toLowerCase());
}

modoVerboso = cmd.hasOption("verbose");
modoVerboso = optionCLIReader.isModoVerboso();

if ("pdf".equals(formato)) {
try(PdfWriter writer = new PdfWriter(Files.newOutputStream(arquivoDeSaida));
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/cotuba/cli/OptionCLIReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cotuba.cli;

import org.apache.commons.cli.*;

public class OptionCLIReader {
Copy link
Contributor

Choose a reason for hiding this comment

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

O projeto já estava usando as variáveis em português. Por isso, acho que seria melhor manter o nome da classe em português.


private String nomeDoDiretorioDosMD;
private String nomeDoFormatoDoEbook;
private String nomeDoArquivoDeSaidaDoEbook;
private boolean modoVerboso = false;

public OptionCLIReader(String [] args) {
Options options = new Options();

options.addOption(new Option("d", "dir", true, "Diretório que contem os arquivos md. Default: diretório atual."));
options.addOption(new Option("f", "format", true, "Formato de saída do ebook. Pode ser: pdf ou epub. Default: pdf"));
options.addOption(new Option("o", "output", true, "Arquivo de saída do ebook. Default: book.{formato}."));
options.addOption(new Option("v", "verbose", false, "Habilita modo verboso."));

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;
}

this.nomeDoDiretorioDosMD = cmd.getOptionValue("dir");
this.nomeDoFormatoDoEbook = cmd.getOptionValue("format");
this.nomeDoArquivoDeSaidaDoEbook = cmd.getOptionValue("output");
this.modoVerboso = cmd.hasOption("verbose");
Comment on lines +13 to +36
Copy link
Contributor

Choose a reason for hiding this comment

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

Você acha que seria bom reorganizar esse trecho de código? Se sim, como você faria?

}

public String getNomeDoDiretorioDosMD() {
return nomeDoDiretorioDosMD;
}

public String getNomeDoFormatoDoEbook() {
return nomeDoFormatoDoEbook;
}

public String getNomeDoArquivoDeSaidaDoEbook() {
return nomeDoArquivoDeSaidaDoEbook;
}

public boolean isModoVerboso() {
return modoVerboso;
}
}