<commit_before>package com.github.thetric.iliasdownloader.cli

import com.github.thetric.iliasdownloader.cli.console.ConsoleService
import com.github.thetric.iliasdownloader.cli.console.SystemEnvironmentAwareConsoleService
import com.github.thetric.iliasdownloader.service.IliasService
import com.github.thetric.iliasdownloader.service.webparser.WebParserIliasServiceProvider
import com.github.thetric.iliasdownloader.ui.common.prefs.JsonUserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import groovy.transform.TupleConstructor
import groovy.util.logging.Log4j2

import java.nio.file.Path
import java.nio.file.Paths
import java.util.function.Function

import static org.apache.logging.log4j.Level.TRACE

@Log4j2
@TupleConstructor
final class Cli {
    // NOTE DO NOT launch in IntelliJ with 'Delegate IDE build/run actions to gradle' as no console is then available!
    // instead you should define system environment variables (see IliasCliController for the var names)

    ResourceBundle resourceBundle

    static void main(String[] args) {
        try {
            ResourceBundle resourceBundle = ResourceBundle.getBundle('ilias-cli')
            new Cli(resourceBundle).parseAndHandleOpts(args)
        } catch (InvalidUsageException ue) {
            log.catching(TRACE, ue)
        }
    }

    private void parseAndHandleOpts(String[] args) {
        CliBuilder cliBuilder = createCliBuilder()
        final opts = cliBuilder.parse(args)
        if (opts) {
            handleOptsReal(cliBuilder.parse(args))
        } else {
            throw new InvalidUsageException()
        }
    }

    private CliBuilder createCliBuilder() {
        final cliBuilder = new CliBuilder()
        cliBuilder.header = 'Ilias Downloader (CLI)'
        cliBuilder.d(
            longOpt: 'dir',
            argName: resourceBundle.getString('args.directory'),
            args: 1,
            required: true,
            resourceBundle.getString('args.directory.description'))
        cliBuilder.c(
            longOpt: 'select-courses',
            required: false,
            args: 0,
            resourceBundle.getString('args.course.selection'))
        cliBuilder.s(
            longOpt: 'max-size',
            required: false,
            args: 1,
            resourceBundle.getString('args.sync.max-size')
        )
        return cliBuilder
    }

    private void handleOptsReal(OptionAccessor options) {
        long size = options.s == false ? 0 : options.s as Long
        CliOptions cliOptions = new CliOptions(
            syncDir: Paths.get(options.d as String),
            showCourseSelection: options.c as boolean,
            fileSizeLimitInMiB: size,
        )
        Path settingsPath = cliOptions.syncDir.resolve('.ilias-downloader.json')
        UserPreferenceService preferenceService = new JsonUserPreferenceService(settingsPath)
        final ConsoleService consoleService = new SystemEnvironmentAwareConsoleService()
        Function<String, IliasService> webIliasServiceProvider = { new WebParserIliasServiceProvider(it).newInstance() }
<commit_msg>fix: do not reset maxFileSize to 0<commit_after>package com.github.thetric.iliasdownloader.cli

import com.github.thetric.iliasdownloader.cli.console.ConsoleService
import com.github.thetric.iliasdownloader.cli.console.SystemEnvironmentAwareConsoleService
import com.github.thetric.iliasdownloader.service.IliasService
import com.github.thetric.iliasdownloader.service.webparser.WebParserIliasServiceProvider
import com.github.thetric.iliasdownloader.ui.common.prefs.JsonUserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import groovy.transform.TupleConstructor
import groovy.util.logging.Log4j2

import java.nio.file.Path
import java.nio.file.Paths
import java.util.function.Function

import static org.apache.logging.log4j.Level.TRACE

@Log4j2
@TupleConstructor
final class Cli {
    // NOTE DO NOT launch in IntelliJ with 'Delegate IDE build/run actions to gradle' as no console is then available!
    // instead you should define system environment variables (see IliasCliController for the var names)

    ResourceBundle resourceBundle

    static void main(String[] args) {
        try {
            ResourceBundle resourceBundle = ResourceBundle.getBundle('ilias-cli')
            new Cli(resourceBundle).parseAndHandleOpts(args)
        } catch (InvalidUsageException ue) {
            log.catching(TRACE, ue)
        }
    }

    private void parseAndHandleOpts(String[] args) {
        CliBuilder cliBuilder = createCliBuilder()
        final opts = cliBuilder.parse(args)
        if (opts) {
            handleOptsReal(cliBuilder.parse(args))
        } else {
            throw new InvalidUsageException()
        }
    }

    private CliBuilder createCliBuilder() {
        final cliBuilder = new CliBuilder()
        cliBuilder.header = 'Ilias Downloader (CLI)'
        cliBuilder.d(
            longOpt: 'dir',
            argName: resourceBundle.getString('args.directory'),
            args: 1,
            required: true,
            resourceBundle.getString('args.directory.description'))
        cliBuilder.c(
            longOpt: 'select-courses',
            required: false,
            args: 0,
            resourceBundle.getString('args.course.selection'))
        cliBuilder.s(
            longOpt: 'max-size',
            required: false,
            args: 1,
            resourceBundle.getString('args.sync.max-size')
        )
        return cliBuilder
    }

    private void handleOptsReal(OptionAccessor options) {
        Long size = options.s == false ? null : options.s as Long
        CliOptions cliOptions = new CliOptions(
            syncDir: Paths.get(options.d as String),
            showCourseSelection: options.c as boolean,
            fileSizeLimitInMiB: size,
        )
        Path settingsPath = cliOptions.syncDir.resolve('.ilias-downloader.json')
        UserPreferenceService preferenceService = new JsonUserPreferenceService(settingsPath)
        final ConsoleService consoleService = new SystemEnvironmentAwareConsoleService()
        Function<String, IliasService> webIliasServiceProvider = { new WebParserIliasServiceProvider(it).newInstance() }

{'fraction_non_alphanumeric': 0.08261700095510983, 'fraction_numerical': 0.0028653295128939827, 'mean_word_length': 2.9441305712492154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831289', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1691, 'n_words': 366}