Skip to content

Commit

Permalink
Allow to set default options with $BLOBDROP_ARGS
Browse files Browse the repository at this point in the history
This is the easiest way to implement setting default values without
having to implement an entire config system.
  • Loading branch information
vimpostor committed Jul 25, 2024
1 parent bc472b5 commit 154053b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (UNIX AND NOT APPLE)
endif()

include(FetchContent)
FetchContent_Declare(quartz GIT_REPOSITORY https://github.com/vimpostor/quartz.git GIT_TAG 9f1ac6cce6b338c6613aa195cffe6f0bb5c965df)
FetchContent_Declare(quartz GIT_REPOSITORY https://github.com/vimpostor/quartz.git GIT_TAG v0.7)
FetchContent_MakeAvailable(quartz)

list(APPEND LINK_LIBS ${PKGCONFIG_MODULES})
Expand Down
16 changes: 16 additions & 0 deletions doc/man/man1/blobdrop.1
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ Using this option causes blobdrop to quit after the first drag operation has fin
.SS "all"
With this option blobdrop keeps track of which items have been dragged already. It quits when all paths have been dragged at least once.

.SH DEFAULT ARGUMENTS
The
.B $BLOBDROP_ARGS
environment variable can be used to provide default arguments. The default arguments will be prepended to the actually passed arguments, for example:
.PP
.in +2n
.EX
$ \fBBLOBDROP_ARGS\fP=\fI"\-f gui \-p"\fP \fBblobdrop\fP \-x \fInever\fP image.png
$ # is equivalent to:
$ \fBblobdrop\fP \-f \fIgui\fP \-p \-x \fInever\fP image.png
.EE
.in
.PP

This can be useful to change the default value of some options permanently.

.SH EXAMPLES
Here are some example usecases.

Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
owner = "vimpostor";
repo = "quartz";
rev = builtins.head (builtins.match ".*FetchContent_Declare\\(.*GIT_TAG ([[:alnum:]\\.]+).*" (builtins.readFile ./CMakeLists.txt));
hash = "sha256-UacYQ5c+MGUK4saLohnZs4691CBGI59JpkdgCaYbPUk=";
hash = "sha256-cANBwVXcnWPaFE58lfbi53DUJ1mAmJL/p1hxoS5cX7s=";
};
makeStdenvPkg = env: env.mkDerivation {
pname = "blobdrop";
Expand Down
10 changes: 7 additions & 3 deletions src/getopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

namespace Getopts {

bool parse(QCoreApplication &app) {
QStringList setup_args(int argc, char *argv[]) {
const auto *env = std::getenv("BLOBDROP_ARGS");
return quartz::getopts::prepend_args(argc, argv, env);
}

bool parse(const QStringList &args) {
QCommandLineParser p;
p.setApplicationDescription("Quickly drag and drop files from the terminal to applications.");
p.addHelpOption();
Expand Down Expand Up @@ -49,7 +54,7 @@ bool parse(QCoreApplication &app) {
"behaviour");

p.addOptions({frameless_opt, cursor_opt, frontend_opt, intercept_opt, keep_opt, persistent_opt, remote_opt, ontop_opt, auto_quit_opt});
p.process(app);
p.process(args);

if (p.isSet(auto_quit_opt)) {
const auto opt = p.value(auto_quit_opt);
Expand Down Expand Up @@ -90,5 +95,4 @@ bool parse(QCoreApplication &app) {
std::ranges::for_each(p.positionalArguments(), [](auto i) { PathRegistry::get()->add_path(i.toStdString()); });
return true;
}

}
5 changes: 4 additions & 1 deletion src/getopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

#include <QCommandLineParser>
#include <iostream>
#include <quartz/getopts.hpp>

#include "path_registry.hpp"
#include "settings.hpp"

namespace Getopts {
bool parse(QCoreApplication &app);

QStringList setup_args(int argc, char *argv[]);
bool parse(const QStringList &args);
}
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ int main(int argc, char *argv[]) {
quartz::Signals signal_handler {{SIGINT, SIGHUP, SIGTERM, SIGQUIT}, [](int) { Backend::get()->quit_delayed(0ms); }};
#endif

if (!Getopts::parse(app)) {
const auto &args = Getopts::setup_args(argc, argv);
if (!Getopts::parse(args)) {
return EXIT_FAILURE;
}

Expand Down

0 comments on commit 154053b

Please sign in to comment.