Skip to content

Commit

Permalink
refactor: move ArgumentParser to own directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed Jan 31, 2023
1 parent 4569c2d commit c77b227
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lib/active_window/src/active_window.dart
@@ -1,7 +1,7 @@
import 'package:flutter/foundation.dart';

import '../../argument_parser/argument_parser.dart';
import '../../logs/logs.dart';
import '../../main.dart';
import '../../native_platform/native_platform.dart';
import '../../storage/storage_repository.dart';

Expand Down
81 changes: 81 additions & 0 deletions lib/argument_parser/argument_parser.dart
@@ -0,0 +1,81 @@
import 'dart:io';

import 'package:args/args.dart';

/// Message to be displayed if Nyrna is called with an unknown argument.
const _helpTextGreeting = '''
Nyrna - Suspend games and applications.
Run Nyrna without any arguments to launch the GUI.
Supported arguments:
''';

/// Parse command-line arguments.
class ArgumentParser {
bool? minimize;
bool toggleActiveWindow = false;
bool verbose = false;

/// Singleton instance.
static late ArgumentParser instance;

ArgumentParser() {
instance = this;
}

final _parser = ArgParser(usageLineLength: 80);

/// Parse received arguments.
void parseArgs(List<String> args) {
_parser
..addFlag(
'minimize',
defaultsTo: true,
callback: (bool value) {
/// We only want to register when the user calls the negated version of
/// this flag: `--no-minimize`. Otherwise the [minimize] value will be
/// null and the UI-set preference can be checked.
if (value == true) {
return;
} else {
minimize = false;
}
},
help: '''
Used with the `toggle` flag, `no-minimize` instructs Nyrna not to automatically minimize / restore the active window - it will be suspended / resumed only.''',
)
..addFlag(
'toggle',
abbr: 't',
negatable: false,
callback: (bool value) => toggleActiveWindow = value,
help: 'Toggle the suspend / resume state for the active window. \n'
'❗Please note this will immediately suspend the active window, and '
'is intended to be used with a hotkey - be sure not to run this '
'from a terminal and accidentally suspend your terminal! ❗',
)
..addFlag(
'verbose',
abbr: 'v',
negatable: false,
callback: (bool value) => verbose = value,
help: 'Output verbose logs for troubleshooting and debugging.',
);

final helpText = '$_helpTextGreeting${_parser.usage}\n\n';

try {
final result = _parser.parse(args);
if (result.rest.isNotEmpty) {
stdout.writeln(helpText);
exit(0);
}
} on ArgParserException {
stdout.writeln(helpText);
exit(0);
}
}
}
80 changes: 1 addition & 79 deletions lib/main.dart
@@ -1,6 +1,5 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:desktop_integration/desktop_integration.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand All @@ -15,6 +14,7 @@ import 'app.dart';
import 'app/app.dart';
import 'app_version/app_version.dart';
import 'apps_list/apps_list.dart';
import 'argument_parser/argument_parser.dart';
import 'hotkey/hotkey_service.dart';
import 'logs/logs.dart';
import 'native_platform/native_platform.dart';
Expand Down Expand Up @@ -111,84 +111,6 @@ Future<void> main(List<String> args) async {
if (startHiddenInTray != true) await appWindow.show();
}

/// Message to be displayed if Nyrna is called with an unknown argument.
const _helpTextGreeting = '''
Nyrna - Suspend games and applications.
Run Nyrna without any arguments to launch the GUI.
Supported arguments:
''';

/// Parse command-line arguments.
class ArgumentParser {
bool? minimize;
bool toggleActiveWindow = false;
bool verbose = false;

/// Singleton instance.
static late ArgumentParser instance;

ArgumentParser() {
instance = this;
}

final _parser = ArgParser(usageLineLength: 80);

/// Parse received arguments.
void parseArgs(List<String> args) {
_parser
..addFlag(
'minimize',
defaultsTo: true,
callback: (bool value) {
/// We only want to register when the user calls the negated version of
/// this flag: `--no-minimize`. Otherwise the [minimize] value will be
/// null and the UI-set preference can be checked.
if (value == true) {
return;
} else {
minimize = false;
}
},
help: '''
Used with the `toggle` flag, `no-minimize` instructs Nyrna not to automatically minimize / restore the active window - it will be suspended / resumed only.''',
)
..addFlag(
'toggle',
abbr: 't',
negatable: false,
callback: (bool value) => toggleActiveWindow = value,
help: 'Toggle the suspend / resume state for the active window. \n'
'❗Please note this will immediately suspend the active window, and '
'is intended to be used with a hotkey - be sure not to run this '
'from a terminal and accidentally suspend your terminal! ❗',
)
..addFlag(
'verbose',
abbr: 'v',
negatable: false,
callback: (bool value) => verbose = value,
help: 'Output verbose logs for troubleshooting and debugging.',
);

final helpText = '$_helpTextGreeting${_parser.usage}\n\n';

try {
final result = _parser.parse(args);
if (result.rest.isNotEmpty) {
stdout.writeln(helpText);
exit(0);
}
} on ArgParserException {
stdout.writeln(helpText);
exit(0);
}
}
}

/// Instantiates DesktopIntegration for DI.
Future<DesktopIntegration> _initDesktopIntegration() async {
File? desktopFile;
Expand Down
2 changes: 1 addition & 1 deletion test/active_window/src/active_window_test.dart
@@ -1,8 +1,8 @@
import 'package:flutter/foundation.dart';
import 'package:mocktail/mocktail.dart';
import 'package:nyrna/active_window/active_window.dart';
import 'package:nyrna/argument_parser/argument_parser.dart';
import 'package:nyrna/logs/logs.dart';
import 'package:nyrna/main.dart';
import 'package:nyrna/native_platform/native_platform.dart';
import 'package:nyrna/storage/storage_repository.dart';
import 'package:test/test.dart';
Expand Down
2 changes: 1 addition & 1 deletion test/argument_parser/argument_parser_test.dart
@@ -1,4 +1,4 @@
import 'package:nyrna/main.dart';
import 'package:nyrna/argument_parser/argument_parser.dart';
import 'package:test/test.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion test/domain/arguments/argument_parser_test.dart
@@ -1,5 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:nyrna/main.dart';
import 'package:nyrna/argument_parser/argument_parser.dart';

void main() {
group('ArgumentParser:', () {
Expand Down

0 comments on commit c77b227

Please sign in to comment.