Skip to content

Commit 234ae35

Browse files
author
John Keyes
committed
- rewrote i18n code
- externalised error strings git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@280462 13f79535-47bb-0310-9956-ffa450edef68
1 parent d7b3db1 commit 234ae35

40 files changed

Lines changed: 2113 additions & 2100 deletions

src/java/org/apache/commons/cli2/OptionException.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
1+
/*
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,44 +23,42 @@
2323
/**
2424
* A problem found while dealing with command line options.
2525
*/
26-
public class OptionException extends Exception {
27-
26+
public class OptionException
27+
extends Exception {
2828
/**
2929
* The settings used when displaying the related Option.
30-
*
30+
*
3131
* @see DisplaySetting
3232
*/
33-
public static final Set HELP_SETTINGS =
34-
Collections.unmodifiableSet(
35-
Collections.singleton(
36-
DisplaySetting.DISPLAY_PROPERTY_OPTION));
33+
public static final Set HELP_SETTINGS =
34+
Collections.unmodifiableSet(Collections.singleton(DisplaySetting.DISPLAY_PROPERTY_OPTION));
35+
36+
/** resource helper instance */
37+
private static final ResourceHelper helper = ResourceHelper.getResourceHelper();
3738

3839
/** The Option the exception relates to */
3940
private final Option option;
40-
41+
4142
/** The message explaining the Exception */
4243
private final String message;
4344

44-
/** resource helper instance */
45-
private static final ResourceHelper helper =
46-
ResourceHelper.getResourceHelper(OptionException.class);
47-
4845
/**
4946
* Creates a new OptionException.
50-
*
47+
*
5148
* @param option
5249
* The Option the exception relates to
5350
*/
5451
public OptionException(final Option option) {
5552
this(option, null, null);
5653
}
57-
54+
5855
/**
5956
* Creates a new OptionException.
6057
* @param option the Option the exception relates to
6158
* @param messageKey the id of the message to display
6259
*/
63-
public OptionException(final Option option, final String messageKey) {
60+
public OptionException(final Option option,
61+
final String messageKey) {
6462
this(option, messageKey, null);
6563
}
6664

@@ -70,40 +68,38 @@ public OptionException(final Option option, final String messageKey) {
7068
* @param messageKey the id of the message to display
7169
* @param value a value to display with the message
7270
*/
73-
public OptionException(
74-
final Option option,
75-
final String messageKey,
76-
final String value) {
77-
71+
public OptionException(final Option option,
72+
final String messageKey,
73+
final String value) {
7874
this.option = option;
79-
75+
8076
if (messageKey != null) {
8177
final StringBuffer buffer = new StringBuffer();
78+
8279
if (value != null) {
8380
buffer.append(helper.getMessage(messageKey, value));
84-
}
85-
else {
81+
} else {
8682
buffer.append(helper.getMessage(messageKey));
8783
}
84+
8885
buffer.append(" ");
8986

9087
option.appendUsage(buffer, HELP_SETTINGS, null);
9188
message = buffer.toString();
92-
}
93-
else {
89+
} else {
9490
message = "";
9591
}
9692
}
97-
93+
9894
/**
9995
* Gets the Option the exception relates to
100-
*
96+
*
10197
* @return The related Option
10298
*/
10399
public Option getOption() {
104100
return option;
105101
}
106-
102+
107103
public String getMessage() {
108104
return message;
109105
}

src/java/org/apache/commons/cli2/builder/CommandBuilder.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright 2003-2004 The Apache Software Foundation
1+
/*
2+
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,12 +21,13 @@
2121
import org.apache.commons.cli2.Argument;
2222
import org.apache.commons.cli2.Group;
2323
import org.apache.commons.cli2.option.Command;
24+
import org.apache.commons.cli2.resource.ResourceConstants;
25+
import org.apache.commons.cli2.resource.ResourceHelper;
2426

2527
/**
2628
* Builds Command instances
2729
*/
2830
public class CommandBuilder {
29-
3031
/** the preferred name of the command */
3132
private String preferredName;
3233

@@ -58,26 +59,18 @@ public CommandBuilder() {
5859
/**
5960
* Creates a new <code>Command</code> instance using the properties of the
6061
* <code>CommandBuilder</code>.
61-
*
62+
*
6263
* @return the new Command instance
6364
*/
6465
public Command create() {
65-
6666
// check we have a valid name
6767
if (preferredName == null) {
68-
throw new IllegalStateException("Options must have at least one name");
68+
throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
6969
}
7070

7171
// build the command
7272
final Command option =
73-
new Command(
74-
preferredName,
75-
description,
76-
aliases,
77-
required,
78-
argument,
79-
children,
80-
id);
73+
new Command(preferredName, description, aliases, required, argument, children, id);
8174

8275
// reset the builder
8376
reset();
@@ -86,8 +79,8 @@ public Command create() {
8679
}
8780

8881
/**
89-
* Resets the CommandBuilder to the defaults for a new Command.
90-
*
82+
* Resets the CommandBuilder to the defaults for a new Command.
83+
*
9184
* This method is called automatically at the end of the
9285
* {@link #create() create} method.
9386
*/
@@ -99,24 +92,24 @@ public CommandBuilder reset() {
9992
argument = null;
10093
children = null;
10194
id = 0;
95+
10296
return this;
10397
}
10498

10599
/**
106-
* Specifies the name for the next <code>Command</code>
100+
* Specifies the name for the next <code>Command</code>
107101
* that is created. The first name is used as the preferred
108-
* display name for the <code>Command</code> and then
102+
* display name for the <code>Command</code> and then
109103
* later names are used as aliases.
110-
*
104+
*
111105
* @param name the name for the next <code>Command</code>
112106
* that is created.
113107
* @return this <code>CommandBuilder</code>.
114108
*/
115109
public CommandBuilder withName(final String name) {
116110
if (preferredName == null) {
117111
preferredName = name;
118-
}
119-
else {
112+
} else {
120113
aliases.add(name);
121114
}
122115

@@ -125,15 +118,16 @@ public CommandBuilder withName(final String name) {
125118

126119
/**
127120
* Specifies the description for the next <code>Command</code>
128-
* that is created. This description is used to produce
121+
* that is created. This description is used to produce
129122
* help documentation for the <code>Command</code>.
130-
*
131-
* @param newDescription the description for the next
123+
*
124+
* @param newDescription the description for the next
132125
* <code>Command</code> that is created.
133126
* @return this <code>CommandBuilder</code>.
134127
*/
135128
public CommandBuilder withDescription(final String newDescription) {
136129
this.description = newDescription;
130+
137131
return this;
138132
}
139133

@@ -146,43 +140,47 @@ public CommandBuilder withDescription(final String newDescription) {
146140
*/
147141
public CommandBuilder withRequired(final boolean newRequired) {
148142
this.required = newRequired;
143+
149144
return this;
150145
}
151146

152147
/**
153-
* Specifies the children for the next <code>Command</code>
148+
* Specifies the children for the next <code>Command</code>
154149
* that is created.
155-
*
150+
*
156151
* @param newChildren the child options for the next <code>Command</code>
157152
* that is created.
158153
* @return this <code>CommandBuilder</code>.
159154
*/
160155
public CommandBuilder withChildren(final Group newChildren) {
161156
this.children = newChildren;
157+
162158
return this;
163159
}
164160

165161
/**
166-
* Specifies the argument for the next <code>Command</code>
162+
* Specifies the argument for the next <code>Command</code>
167163
* that is created.
168-
*
169-
* @param newArgument the argument for the next <code>Command</code>
164+
*
165+
* @param newArgument the argument for the next <code>Command</code>
170166
* that is created.
171167
* @return this <code>CommandBuilder</code>.
172168
*/
173169
public CommandBuilder withArgument(final Argument newArgument) {
174170
this.argument = newArgument;
171+
175172
return this;
176173
}
177174

178175
/**
179176
* Specifies the id for the next <code>Command</code> that is created.
180-
*
177+
*
181178
* @param newId the id for the next <code>Command</code> that is created.
182179
* @return this <code>CommandBuilder</code>.
183180
*/
184181
public final CommandBuilder withId(final int newId) {
185182
this.id = newId;
183+
186184
return this;
187185
}
188186
}

0 commit comments

Comments
 (0)