cli/command_line/internal/Help.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
4b873ebb5db7c93a87271655570c9c0fcefd06c5
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Private command-line help implementation.

renderHelp: (rootSpec, commandPath) => {
    validateArray: (value) => {
        Array.recognizes(value).ifFalse(() => {
            fail()
        })
        value
    }

    validateString: (value) => {
        String.recognizes(value).ifFalse(() => {
            fail()
        })
        value
    }

    rootSpec.hasSlot("kind").ifFalse(() => {
        fail()
    })
    (rootSpec.slotValue("kind") == "command").ifFalse(() => {
        fail()
    })

    validateArray(commandPath)

    selectedSpec: rootSpec
    for(segment in commandPath) {
        validateString(segment)

        selectedChild: null
        for(childSpec in selectedSpec.subcommands) {
            if(
                (selectedChild === null) &&
                (childSpec.name == segment)
            ) {
                selectedChild = childSpec
            }
        }

        if(selectedChild === null) {
            fail()
        }

        selectedSpec = selectedChild
    }

    output: Encoding.UTF8.encode("")

    appendText: (text) => {
        source: Encoding.UTF8.encode(text)
        index: 0
        while(index < source.size()) {
            output.add(source[index])
            index = index + 1
        }
    }

    optionUsageTerm: (optionSpec) => {
        spelling: ""
        (optionSpec.long === null).ifFalse(() => {
            spelling = "--" + optionSpec.long
        })
        if(optionSpec.long === null) {
            spelling = "-" + optionSpec.short
        }

        if(optionSpec.maxValues == 1) {
            spelling = spelling + " " + optionSpec.valueName
        }

        spelling
    }

    positionalTerm: (positionalSpec) => {
        rendered: positionalSpec.valueName

        if(
            (positionalSpec.minOccurrences == 0) &&
            (positionalSpec.maxOccurrences == 1)
        ) {
            rendered = "[" + positionalSpec.valueName + "]"
        }

        if(
            (positionalSpec.minOccurrences == 0) &&
            (positionalSpec.maxOccurrences === null)
        ) {
            rendered = "[" + positionalSpec.valueName + "]..."
        }

        if(
            (positionalSpec.minOccurrences == 1) &&
            (positionalSpec.maxOccurrences === null)
        ) {
            rendered =
                positionalSpec.valueName +
                " [" + positionalSpec.valueName + "]..."
        }

        rendered
    }

    optionEntry: (optionSpec) => {
        rendered: ""

        (optionSpec.short === null).ifFalse(() => {
            rendered = "-" + optionSpec.short
        })

        (optionSpec.long === null).ifFalse(() => {
            (rendered == "").ifFalse(() => {
                rendered = rendered + ", "
            })
            rendered = rendered + "--" + optionSpec.long
        })

        if(optionSpec.maxValues == 1) {
            rendered = rendered + " " + optionSpec.valueName
        }

        if(
            (optionSpec.minOccurrences == 1) &&
            (optionSpec.maxOccurrences == 1)
        ) {
            rendered = rendered + " (required)"
        }

        if(
            (optionSpec.minOccurrences == 0) &&
            (optionSpec.maxOccurrences === null)
        ) {
            rendered = rendered + " (repeatable)"
        }

        if(
            (optionSpec.minOccurrences == 1) &&
            (optionSpec.maxOccurrences === null)
        ) {
            rendered = rendered + " (required, repeatable)"
        }

        rendered
    }

    appendDescription: (helpText) => {
        (helpText === null).ifFalse(() => {
            appendText("\n    ")
            appendText(helpText)
        })
    }

    appendText("Usage: ")
    appendText(rootSpec.name)

    for(segment in commandPath) {
        appendText(" ")
        appendText(segment)
    }

    hasOptionalOptions: false
    for(optionSpec in selectedSpec.options) {
        if(optionSpec.minOccurrences == 0) {
            hasOptionalOptions = true
        }

        if(optionSpec.minOccurrences == 1) {
            term: optionUsageTerm(optionSpec)
            appendText(" ")
            appendText(term)

            if(optionSpec.maxOccurrences === null) {
                appendText(" [")
                appendText(term)
                appendText("]...")
            }
        }
    }

    if(hasOptionalOptions) {
        appendText(" [OPTIONS]")
    }

    for(positionalSpec in selectedSpec.positionals) {
        appendText(" ")
        appendText(positionalTerm(positionalSpec))
    }

    if(selectedSpec.subcommands.size() > 0) {
        appendText(" [COMMAND]")
    }

    (selectedSpec.help === null).ifFalse(() => {
        appendText("\n\n")
        appendText(selectedSpec.help)
    })

    if(selectedSpec.positionals.size() > 0) {
        appendText("\n\nArguments:")
        for(positionalSpec in selectedSpec.positionals) {
            appendText("\n  ")
            appendText(positionalTerm(positionalSpec))
            appendDescription(positionalSpec.help)
        }
    }

    if(selectedSpec.options.size() > 0) {
        appendText("\n\nOptions:")
        for(optionSpec in selectedSpec.options) {
            appendText("\n  ")
            appendText(optionEntry(optionSpec))
            appendDescription(optionSpec.help)
        }
    }

    if(selectedSpec.subcommands.size() > 0) {
        appendText("\n\nCommands:")
        for(childSpec in selectedSpec.subcommands) {
            appendText("\n  ")
            appendText(childSpec.name)
            appendDescription(childSpec.help)
        }
    }

    Encoding.UTF8.decode(output)
}