forked from auberonedu/truffula
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTruffulaOptions.java
More file actions
160 lines (147 loc) · 4.57 KB
/
TruffulaOptions.java
File metadata and controls
160 lines (147 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.io.File;
import java.io.FileNotFoundException;
/**
* Represents configuration options for controlling how a directory tree is displayed.
*
* Options include:
* - Whether to show hidden files.
* - Whether to use colored output.
* - The root directory from which to begin printing the tree.
*
* Hidden files are identified by names that start with a dot (e.g., ".hidden.txt").
* Color output is enabled by default, but can be disabled using flags.
*
* Usage Example:
*
* Arguments Format: [-h] [-nc] path
*
* Flags:
* - -h : Show hidden files (defaults to false).
* - -nc : Do not use color (color is enabled by default).
*
* Path:
* - The absolute or relative path to the directory whose contents will be printed.
*
* Behavior:
* - If color is disabled, all text will be printed in white.
* - The order of flags is unimportant.
* - The path argument is mandatory.
*
* Examples:
*
* 1. ['-nc', '-h', '/path/to/directory']
* → Don't use color, do show hidden files.
*
* 2. ['-h', '-nc', '/path/to/directory']
* → Don't use color, do show hidden files (order of flags is ignored).
*
* 3. ['/path/to/directory']
* → Use color, don't show hidden files.
*
* Exceptions:
* - Throws IllegalArgumentException if:
* - Unknown flags are provided.
* - The path argument is missing.
*
* - Throws FileNotFoundException if:
* - The specified directory does not exist.
* - The path points to a file instead of a directory.
*/
public class TruffulaOptions {
private final File root;
private final boolean showHidden;
private final boolean useColor;
/**
* Returns the root directory from which the directory tree will be printed.
*
* @return the root directory as a File object
*/
public File getRoot() {
return root;
}
/**
* Indicates whether hidden files should be included when printing the directory tree.
*
* @return true if hidden files should be shown; false otherwise
*/
public boolean isShowHidden() {
return showHidden;
}
@Override
public String toString() {
return "TruffulaOptions [root=" + root + ", showHidden=" + showHidden + ", useColor=" + useColor + "]";
}
/**
* Indicates whether color should be used when printing the directory tree.
*
* If false, all output is printed in white.
*
* @return true if color should be used; false otherwise
*/
public boolean isUseColor() {
return useColor;
}
/**
* Constructs a TruffulaOptions object based on command-line arguments.
*
* Supported Flags:
* - -h : Show hidden files (defaults to false).
* - -nc : Do not use color (uses color by default).
*
* The last argument must be the path to the directory.
*
* @param args command-line arguments in the format [-h] [-nc] path
* @throws IllegalArgumentException if unknown arguments are provided or the path is missing
* @throws FileNotFoundException if the directory cannot be found or if the path points to a file
*/
public TruffulaOptions(String[] args) throws IllegalArgumentException, FileNotFoundException {
// TODO: Replace the below lines with your implementation
if (args.length == 0) {
throw new IllegalArgumentException("No arguments provided.");
}
boolean hiddenFile = false;
boolean colors = true;
for (int i = 0; i < args.length - 1; i++) {
if (args[i].equals("-h")) {
hiddenFile = true;
}
else if (args[i].equals("-nc")) {
colors = false;
}
else {
throw new IllegalArgumentException("Unknown flag: " + args[i]);
}
}
File file = null;
if (!args[args.length - 1].equals("-nc") && !args[args.length - 1].equals("-h")) {
file = new File(args[args.length - 1]);
}
else {
file = null;
}
showHidden = hiddenFile;
useColor = colors;
root = file;
if (root == null) {
throw new IllegalArgumentException("Path is missing");
}
if (!root.exists()) {
throw new FileNotFoundException("Could not find directory");
}
if (!root.isDirectory()) {
throw new FileNotFoundException("Path is not directory");
}
}
/**
* Constructs a TruffulaOptions object with explicit values.
*
* @param root the root directory for the directory tree
* @param showHidden whether hidden files should be displayed
* @param useColor whether color should be used in the output
*/
public TruffulaOptions(File root, boolean showHidden, boolean useColor) {
this.root = root;
this.showHidden = showHidden;
this.useColor = useColor;
}
}