Skip to content

Commit

Permalink
Allow to specify the @file charset
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Sep 12, 2016
1 parent 09e0f59 commit 20a592e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ <h2><a class="section" name="Syntax">@ syntax</a></h2>
java Main @/tmp/parameters
</pre>

<p>The file is read using the default charset unless <code>JCommander#setAtFileCharset</code> had been called.</p>

<p>Ths feature can be disabled by calling <code>JCommander#setExpandAtSign</code>.</p>

<h2><a class="section" name="Arities">Arities (multiple values for parameters)</a></h2>

<h3><a class="section" name="fixed-arities" indent="..">Fixed arities</a></h3>
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package com.beust.jcommander;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -171,6 +173,7 @@ public int compare(ParameterDescription p0, ParameterDescription p1) {
* The factories used to look up string converters.
*/
private final List<IStringConverterInstanceFactory> m_converterInstanceFactories = new CopyOnWriteArrayList<>();
private Charset m_atFileCharset = Charset.defaultCharset();
}

private JCommander(Options options) {
Expand Down Expand Up @@ -224,6 +227,12 @@ public JCommander(Object object, String... args) {
parse(args);
}

/**
* Disables expanding {@code @file}.
*
* JCommander supports the {@code @file} syntax, which allows you to put all your options into a file and pass this file as parameter
* @param expandAtSign whether to expand {@code @file}.
*/
public void setExpandAtSign(boolean expandAtSign){
options.expandAtSign = expandAtSign;
}
Expand Down Expand Up @@ -484,23 +493,18 @@ private String getOptionPrefixes(String arg) {
* @param fileName the command line filename
* @return the file content as a string.
*/
private static List<String> readFile(String fileName) {
private List<String> readFile(String fileName) {
List<String> result = Lists.newArrayList();

try {
BufferedReader bufRead = new BufferedReader(new FileReader(fileName));

try (BufferedReader bufRead = Files.newBufferedReader(Paths.get(fileName), options.m_atFileCharset)) {
String line;

// Read through file one line at time. Print line # and line
while ((line = bufRead.readLine()) != null) {
// Allow empty lines and # comments in these at files
if (line.length() > 0 && ! line.trim().startsWith("#")) {
result.add(line);
}
}

bufRead.close();
}
catch (IOException e) {
throw new ParameterException("Could not read file " + fileName + ": " + e);
Expand Down Expand Up @@ -1647,8 +1651,14 @@ public void setAllowParameterOverwriting(boolean b) {
public boolean isParameterOverwritingAllowed() {
return options.m_allowParameterOverwriting;
}
// public void setCaseSensitiveCommands(boolean b) {
// m_caseSensitiveCommands = b;
// }

/**
* Sets the charset used to expand {@code @files}.
* @param charset the charset
*/
public void setAtFileCharset(Charset charset) {
options.m_atFileCharset = charset;
}

}

26 changes: 26 additions & 0 deletions src/test/java/com/beust/jcommander/JCommanderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -644,6 +647,29 @@ public void atFileCanContainEmptyLines() throws IOException {
new JCommander(new Args1(), "@" + f.getAbsolutePath());
}

public void atFileWithInNonDefaultCharset() throws IOException {
final Charset utf32 = Charset.forName("UTF-32");
final File f = File.createTempFile("JCommander", null);
f.deleteOnExit();
try (OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(f), utf32)) {
fw.write("-log\n");
fw.write("2\n");
fw.write("-groups\n");
fw.write("\u9731\n");
}
final Args1 args1 = new Args1();
final JCommander jc = new JCommander(args1);
try {
jc.parse("@" + f.getAbsolutePath());
throw new IllegalStateException("Expected exception to be thrown");
} catch (ParameterException expected) {
Assert.assertTrue(expected.getMessage().startsWith("Could not read file"));
}
jc.setAtFileCharset(utf32);
jc.parse("@" + f.getAbsolutePath());
Assert.assertEquals("\u9731", args1.groups);
}

public void handleEqualSigns() {
ArgsEquals a = new ArgsEquals();
JCommander jc = new JCommander(a);
Expand Down

0 comments on commit 20a592e

Please sign in to comment.