function checkUsage(e){var s=!1;process._exit=process.exit;process._env=process.env;process._argv=process.argv;process.exit=function(){s=!0};process.env=Hash.merge(process.env,{_:"node"});process.argv=["./usage"];var o=[],t=[];console._error=console.error;console.error=function(e){o.push(e)};console._log=console.log;console.log=function(e){t.push(e)};var a=e();process.exit=process._exit;process.env=process._env;process.argv=process._argv;console.error=console._error;console.log=console._log;return{errors:o,logs:t,exit:s,result:a}}var Hash=require("hashish"),optimist=require("../index"),test=require("tap").test;test("usageFail",function(e){var s=checkUsage(function(){return optimist("-x 10 -z 20".split(" ")).usage("Usage: $0 -x NUM -y NUM").demand(["x","y"]).argv});e.same(s.result,{x:10,z:20,_:[],$0:"./usage"});e.same(s.errors.join("\n").split(/\n+/),["Usage: ./usage -x NUM -y NUM","Options:","  -x  [required]","  -y  [required]","Missing required arguments: y"]);e.same(s.logs,[]);e.ok(s.exit);e.end()});test("usagePass",function(e){var s=checkUsage(function(){return optimist("-x 10 -y 20".split(" ")).usage("Usage: $0 -x NUM -y NUM").demand(["x","y"]).argv});e.same(s,{result:{x:10,y:20,_:[],$0:"./usage"},errors:[],logs:[],exit:!1});e.end()});test("checkPass",function(e){var s=checkUsage(function(){return optimist("-x 10 -y 20".split(" ")).usage("Usage: $0 -x NUM -y NUM").check(function(e){if(!("x"in e))throw"You forgot about -x";if(!("y"in e))throw"You forgot about -y"}).argv});e.same(s,{result:{x:10,y:20,_:[],$0:"./usage"},errors:[],logs:[],exit:!1});e.end()});test("checkFail",function(e){var s=checkUsage(function(){return optimist("-x 10 -z 20".split(" ")).usage("Usage: $0 -x NUM -y NUM").check(function(e){if(!("x"in e))throw"You forgot about -x";if(!("y"in e))throw"You forgot about -y"}).argv});e.same(s.result,{x:10,z:20,_:[],$0:"./usage"});e.same(s.errors.join("\n").split(/\n+/),["Usage: ./usage -x NUM -y NUM","You forgot about -y"]);e.same(s.logs,[]);e.ok(s.exit);e.end()});test("checkCondPass",function(e){function s(e){return"x"in e&&"y"in e}var o=checkUsage(function(){return optimist("-x 10 -y 20".split(" ")).usage("Usage: $0 -x NUM -y NUM").check(s).argv});e.same(o,{result:{x:10,y:20,_:[],$0:"./usage"},errors:[],logs:[],exit:!1});e.end()});test("checkCondFail",function(e){function s(e){return"x"in e&&"y"in e}var o=checkUsage(function(){return optimist("-x 10 -z 20".split(" ")).usage("Usage: $0 -x NUM -y NUM").check(s).argv});e.same(o.result,{x:10,z:20,_:[],$0:"./usage"});e.same(o.errors.join("\n").split(/\n+/).join("\n"),"Usage: ./usage -x NUM -y NUM\nArgument check failed: "+s.toString());e.same(o.logs,[]);e.ok(o.exit);e.end()});test("countPass",function(e){var s=checkUsage(function(){return optimist("1 2 3 --moo".split(" ")).usage("Usage: $0 [x] [y] [z] {OPTIONS}").demand(3).argv});e.same(s,{result:{_:["1","2","3"],moo:!0,$0:"./usage"},errors:[],logs:[],exit:!1});e.end()});test("countFail",function(e){var s=checkUsage(function(){return optimist("1 2 --moo".split(" ")).usage("Usage: $0 [x] [y] [z] {OPTIONS}").demand(3).argv});e.same(s.result,{_:["1","2"],moo:!0,$0:"./usage"});e.same(s.errors.join("\n").split(/\n+/),["Usage: ./usage [x] [y] [z] {OPTIONS}","Not enough non-option arguments: got 2, need at least 3"]);e.same(s.logs,[]);e.ok(s.exit);e.end()});test("defaultSingles",function(e){var s=checkUsage(function(){return optimist("--foo 50 --baz 70 --powsy".split(" "))["default"]("foo",5)["default"]("bar",6)["default"]("baz",7).argv});e.same(s.result,{foo:"50",bar:6,baz:"70",powsy:!0,_:[],$0:"./usage"});e.end()});test("defaultAliases",function(e){var s=checkUsage(function(){return optimist("").alias("f","foo")["default"]("f",5).argv});e.same(s.result,{f:"5",foo:"5",_:[],$0:"./usage"});e.end()});test("defaultHash",function(e){var s=checkUsage(function(){return optimist("--foo 50 --baz 70".split(" "))["default"]({foo:10,bar:20,quux:30}).argv});e.same(s.result,{_:[],$0:"./usage",foo:50,baz:70,bar:20,quux:30});e.end()});test("rebase",function(e){e.equal(optimist.rebase("/home/substack","/home/substack/foo/bar/baz"),"./foo/bar/baz");e.equal(optimist.rebase("/home/substack/foo/bar/baz","/home/substack"),"../../..");e.equal(optimist.rebase("/home/substack/foo","/home/substack/pow/zoom.txt"),"../pow/zoom.txt");e.end()});