@shutootaki/gwm
Version:
git worktree manager CLI
103 lines • 3.38 kB
JavaScript
function hasFlag(args, flags) {
return flags.some((f) => args.includes(f));
}
function getOptionValue(args, option) {
const index = args.indexOf(option);
return index !== -1 && index + 1 < args.length ? args[index + 1] : undefined;
}
function getFirstPositional(args, skip = 1) {
return args.slice(skip).find((a) => !a.startsWith('-'));
}
export function parseAddArgs(args) {
let branchName;
const isRemote = hasFlag(args, ['-r', '--remote']);
const fromBranch = getOptionValue(args, '--from');
const openCode = hasFlag(args, ['--code']);
const openCursor = hasFlag(args, ['--cursor']);
const outputPath = hasFlag(args, ['--cd']);
const skipHooks = hasFlag(args, ['--skip-hooks']);
// 位置引数(ブランチ名)は --from の値を除外した最初の非フラグ
for (let i = 1; i < args.length; i++) {
const arg = args[i];
if (arg === '--from') {
i++; // 次は値なのでスキップ
continue;
}
if (!arg.startsWith('-')) {
branchName = arg;
break;
}
}
return {
branchName,
isRemote,
fromBranch,
openCode,
openCursor,
outputPath,
skipHooks,
};
}
export function parseRemoveArgs(args) {
return {
query: getFirstPositional(args, 1),
force: hasFlag(args, ['-f', '--force']),
cleanBranch: (() => {
let raw = getOptionValue(args, '--clean-branch');
if (!raw) {
const withEq = args.find((a) => a.startsWith('--clean-branch='));
if (withEq) {
raw = withEq.split('=')[1];
}
}
if (raw === 'auto' || raw === 'never')
return raw;
if (raw === 'ask')
return 'ask';
return undefined;
})(),
};
}
export function parseCleanArgs(args) {
// 廃止されたオプションチェック
if (hasFlag(args, ['-y', '--yes', '-i', '--interactive'])) {
throw new Error('Error: --yes/-y および --interactive/-i は廃止されました。代わりに --force を使用してください。');
}
return {
dryRun: hasFlag(args, ['-n', '--dry-run']),
force: hasFlag(args, ['--force']),
};
}
export function parseGoArgs(args) {
let query;
let openCode = false;
let openCursor = false;
for (let i = 1; i < args.length; i++) {
// start from 1 to skip 'go' command
const arg = args[i];
if (arg === '-c' || arg === '--code') {
openCode = true;
}
else if (arg === '--cursor') {
openCursor = true;
}
else if (!arg.startsWith('-') && !query) {
// First non-flag argument is the query
query = arg;
}
}
return { query, openCode, openCursor };
}
export function parsePullMainArgs(_args) {
// 現在はオプションなし、将来の拡張用
return {};
}
export function parseHelpArgs(args) {
// help コマンドの後の最初の位置引数を取得
const command = getFirstPositional(args, 1);
return { command };
}
export function isHelpRequested(args, command) {
return args.includes('--help') || args.includes('-h') || command === 'help';
}
//# sourceMappingURL=cli.js.map