Package org.moeaframework.util.cli
Class UpdateCodeSamples
java.lang.Object
org.moeaframework.util.cli.CommandLineUtility
org.moeaframework.util.cli.UpdateCodeSamples
Utility to update code samples and output found in various documents, including Markdown and HTML files.
The aim is to inject real Java examples into the documentation so that all code snippets can be tested and
validated. This works by embedding a special comment before the code block in one of the supported
UpdateCodeSamples.FileType
. When processing the document, the code block immediately following the comment is validated
or updated from the referenced code.
<!-- java:examples/Example1.java -->
```java
... code block updated from referenced Java file ...
```
The format of the comment is:
<!-- <language>:<filename> [<startingLine>:<endingLine>|<id>] {<flag>,...} -->
<language>
is the name of the programming language. A special case isoutput
, which compiles, executes, and captures the output of the program.[<startingLine>:<endingLine>]
specifies the line numbers, starting at index 1, to extract from the file. If no line numbers are provided, the entire content is copied.- Alternatively, if an identifier is given instead of line numbers, the content enclosed by the comments
// begin-example:<id>
and// end-example:<id>
is copied. {<flag>,...}
specifies additional formatting options, such as{keepComments}
.
-
Constructor Summary
ConstructorDescriptionCreates a new instance of the command line utility to update code examples. -
Method Summary
Modifier and TypeMethodDescriptionorg.apache.commons.cli.Options
Returns the options made available by this command line utility.static void
The main entry point for this command line utility.boolean
Processes a single file, validating or updating code samples.void
run
(org.apache.commons.cli.CommandLine commandLine) Runs this command line utility with the specified command line arguments.Methods inherited from class org.moeaframework.util.cli.CommandLineUtility
createOutputWriter, createOutputWriter, createOutputWriter, fail, getCommandString, getConsoleWidth, getLocalizedOptions, setCommandString, setHideUsage, showHelp, start
-
Constructor Details
-
UpdateCodeSamples
public UpdateCodeSamples()Creates a new instance of the command line utility to update code examples.
-
-
Method Details
-
getOptions
public org.apache.commons.cli.Options getOptions()Description copied from class:CommandLineUtility
Returns the options made available by this command line utility. The base implementation automatically provides the-h,--help
option. Implementations overriding this method and begin with a call tosuper.getOptions()
.- Overrides:
getOptions
in classCommandLineUtility
- Returns:
- the options made available by this command line utility
-
run
Description copied from class:CommandLineUtility
Runs this command line utility with the specified command line arguments.- Specified by:
run
in classCommandLineUtility
- Parameters:
commandLine
- the command line arguments- Throws:
Exception
- if any exception occurred while running this command
-
process
Processes a single file, validating or updating code samples. The file is skipped if the file type is not recognized.- Parameters:
file
- the file to process- Returns:
true
if the file was modified- Throws:
InterruptedException
- if the process was interruptedIOException
- if an I/O error occurred while processing the file
-
main
The main entry point for this command line utility.- Parameters:
args
- the command line arguments- Throws:
Exception
- if an error occurred
-